ios - Retain Cycle Even when using Weak/Strong ARC Semantics -
`i admit not expert on arc , retain cycles though through research , great articles (like this), believe basics.
however, stumped. have property defined follows.
@property (nonatomic,retain) foo *foo;
inside init
, following.
if(self = [super init]) { _foo = [[foo alloc] initwithdelegate:self]; // async rest __weak typeof(self) weakself = self; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, (unsigned long)null), ^(void) { __strong typeof(weakself) strongself = weakself; if (strongself.foo != nil) { [strongself.foo runtests]; } }); } return self; }
and here dealloc
- (void)dealloc { _foo = nil; }
if dispatch_aync
block commented out, see foo dealloc
called after foo set nil
. block commented in, foo's delloc
not called.
i've got retain cycle correct? idea how?
no, not have retain cycle (now known "strong reference cycle" in arc). have code that, if foo
existed time strongself
defined, foo
retained until dispatched code finishes.
the potential strong reference cycle here delegate
passed foo
. if delegate
defined strong
property of foo
class, have strong reference cycle. if it's defined weak
property, have no strong reference cycle.
Comments
Post a Comment