1

`I admit that I am not an expert on ARC and retain cycles though through some research and some great articles (like this), I believe I get the basics.

However, I am currently stumped. I have a property defined as follows.

@property (nonatomic,retain) Foo *foo;

Inside my init, I do the following.

if(self = [super init]) {

    _foo = [[Foo alloc] initWithDelegate:self];

    // async the 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 is my dealloc

- (void)dealloc {
     _foo = nil;
}

If the dispatch_aync block is commented out, I see my Foo dealloc get called immediately after foo is set to nil. With the block commented in, foo's delloc is not called.

I've got a retain cycle correct? Any idea how?

4

1 回答 1

2

不,您不一定有保留周期(现在在 ARC 中称为“强引用周期”)。您有代码,如果在定义foo时已经存在,将一直保留到分派的代码完成。strongSelffoo

这里唯一潜在的强引用循环是delegate你传递给foo. 如果将delegate其定义为类strong的属性Foo,那么您就有一个强大的参考循环。如果它被定义为一个weak属性,那么你就没有强引用循环。

于 2014-09-10T16:09:17.423 回答