Memory leaks with lambda expressions - by CSSForumEngineer

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 704624 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 11/16/2011 11:35:31 AM
Access Restriction Public


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

Memory leaks with lambda expressions

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]

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.
Ben Kuhn

Monday, October 31, 2011 10:25 PM 

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 {
        ~Test() { OutputDebugStringW(L"Disposed\n"); }
        void Dummy() { OutputDebugStringW(L"Dummy\n"); }

delegate void MyDelegate();

int main(array<Platform::String^>^) {
                auto test = ref new Test();
                auto lmbd = [=](){ test->Dummy(); };
                auto dlgt = ref new MyDelegate(lmbd);

Wednesday, November 02, 2011 4:59 PM 
Ben Kuhn [MSFT]
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.
Ben Kuhn
Sign in to post a comment.
Posted by Microsoft on 12/14/2011 at 10:34 AM
    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(