automatic ref counting - Do I need to use a weak pointer when using C++ `function` blocks (as opposed to Objective C blocks) -


if capture strong reference self under arc in objective-c style block, need use __weak pointer avoid arc "retain cycle" problem.

// right way: - (void)configureblock {   xyzblockkeeper * __weak weakself = self;   self.block = ^{     [weakself dosomething];   // capture weak reference                               // avoid reference cycle   } } 

i don't know retain cycle is, this answer describes bit. know should use __weak pointer objective-c style blocks. see avoid strong reference cycles when capturing self.

but question is, need create weak pointer when capturing self under c++ <functional> block?

- (void)configureblock {   self.block = [self](){     [self dosomething]; // ok? it's not objective c block.   } } 

c++ lambdas can captured variables either value or reference (you choose when declare lambda how capture each variable).

capturing reference not interesting, because references local variables become invalid after leave variable's scope anyway, there no memory management issues @ all.

capturing value: if captured variable objective-c object pointer type, gets interesting. if using mrc, nothing happens. if using arc, yes, lambda "retains" captured variables of object pointer type, long __strong (not __weak or __unsafe_unretained). so, yes, create retain cycle.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -