0

我正在处理一个块内使用的对象不会释放的问题。

首先我有这个代码:

__block SOMABannerView* bannerView=_bannerView;
self.viewWillDissappearObserver = [center addObserverForName:UIViewWillDissappearNotification object:self.delegate.viewControllerForPresentingModalView queue:mainQueue usingBlock:
    ^(NSNotification *note) {
        [bannerView setAutoReloadEnabled:NO];
     }]; 

我使用 __block 是因为它不会复制和保留 object ,但是当我用 Instruments 分析这段代码时,我注意到 SOMABannerView 类中的对象没有被释放,所以我将其更改为:

self.viewWillDissappearObserver = [center addObserverForName:UIViewWillDissappearNotification object:self.delegate.viewControllerForPresentingModalView queue:mainQueue usingBlock:
    ^(NSNotification *note) {
        [_bannerView setAutoReloadEnabled:NO];
     }]; 

哪个也不起作用,所以我最终使用 NSNotificationCenter 的另一种方法来避免阻塞,但我仍然不明白为什么 __block 保留了对象,有人可以为我澄清一下吗?我对 __block 有错误的概念吗?

4

1 回答 1

3

它不会在非 ARC 环境中保留对象,但在 ARC 环境中。对于 ARC,使用__weak而不是__block.

资料来源:http ://clang.llvm.org/docs/AutomaticReferenceCounting.html#blocks

于 2013-05-15T02:22:35.687 回答