Home Dashboard Directory Help
Search

Memory leaks with lambda expressions by CSSForumEngineer


Status: 

Closed
 as Fixed Help for as Fixed


1
0
Sign in
to vote
Type: Bug
ID: 704624
Opened: 11/16/2011 11:35:31 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

Submitting on behalf of Metro style aps wih C++:

Memory leaks with lambda expressions
http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/52fb3b64-b5cd-4c1a-957e-a169384ae615


Hello, I have the following code where I'm capturing a variable (test) which is an instance to a ref class.
ThreadPool::RunAsync(ref new WorkItemHandler([test](Windows::Foundation::IAsyncAction^ operation) { ... }))->Start();
C++ variables captured in this way are copied. I assume the instances of ref classes are just retained (using AddRef internally). The problem is that the instance isn't disposed when the lambda expression is destroyed. My understanding is that the instance of WorkItemHandler should be destroyed when the async operation is finished along with the lambda expression which should release all variables that are not captured by reference.
Is this a bug in the preview version of the compiler or am I doing something wrong?

Monday, October 31, 2011 9:13 PM
Ben Kuhn [MSFT]
Microsoft

Your expectations are correct, but we can't reproduce the behavior you are seeing with just the minimal snippet above. Can you provide a complete, minimal repro? What are you observing that leads you to believe there's a missing release call? I ask because it's also possible that some other component wiring is broken, instead.

Thanks,

Ben Kuhn

Monday, October 31, 2011 10:25 PM
Atamiri

Hi Ben, thank you for looking at my problem. In the code below the destructor of Test isn't called. It's called if I use [&] instead of [=].

ref class Test {
public:
        ~Test() { OutputDebugStringW(L"Disposed\n"); }
        void Dummy() { OutputDebugStringW(L"Dummy\n"); }
};

delegate void MyDelegate();

[Platform::MTAThread]
int main(array<Platform::String^>^) {
        {
                auto test = ref new Test();
                auto lmbd = [=](){ test->Dummy(); };
                auto dlgt = ref new MyDelegate(lmbd);
        }
        OutputDebugStringW(L"End\n");
}

Wednesday, November 02, 2011 4:59 PM
Ben Kuhn [MSFT]
Microsoft
Possibly. I've sent this over to the compiler team to have someone take a look. I'll post back here as soon as I get a response.

Thanks,

Ben Kuhn
Details
Sign in to post a comment.
Posted by Microsoft on 12/14/2011 at 10:34 AM
Hi:
    A fix for this issue has been checked into the compiler sources. The fix should show up in the next release of Visual C++.

Xiang Fan
Visual C++ Team
Posted by MS-Moderator07 [Feedback Moderator] on 11/16/2011 at 6:28 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Posted by MS-Moderator01 on 11/16/2011 at 11:45 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.