Using C++11 lambda functions in ARC ObjectiveC++ - how to do it properly? Using C++11 lambda functions in ARC ObjectiveC++ - how to do it properly? objective-c objective-c

Using C++11 lambda functions in ARC ObjectiveC++ - how to do it properly?


The straightforward thing to do would be to let the lambda capture the object pointer variable m_myView (I am assuming from your snippet that this is a local variable), and use it normally inside the lambda:

MyLambdaType myLambda = [m_myView]() {    // Do something with `m_myView`}

The only concern would be the memory management of m_myView. To be generally correct, the lambda needs to retain m_myView when it is created, and release it when it is destroyed (just like blocks do; because the lambda could be used in a scope where m_myView does not exist).

Reading through the ARC docs, I don't see this situation mentioned specifically, but I believe that it should handle it properly, because (1) captured variables of a C++11 lambda are stored as fields of an anonymous class, which are initialized to the captured value when the lambda is constructed, and (2) ARC properly handles the retaining and releasing of Objective-C object fields of C++ classes on construction and destruction. Unless it says something specifically about lambdas to the contrary, or there's a compiler bug, I see no reason why it should not work.