Reputation: 1861
I was dealing with a problem in which an object used inside a block wouldn't release.
First I had this code:
__block SOMABannerView* bannerView=_bannerView;
self.viewWillDissappearObserver = [center addObserverForName:UIViewWillDissappearNotification object:self.delegate.viewControllerForPresentingModalView queue:mainQueue usingBlock:
^(NSNotification *note) {
[bannerView setAutoReloadEnabled:NO];
}];
I used __block because it supposedly wouldn't copy and retain the object , but when I was analyzing this code with Instruments I noticed the objects from class SOMABannerView weren't being deallocated, so I changed it to:
self.viewWillDissappearObserver = [center addObserverForName:UIViewWillDissappearNotification object:self.delegate.viewControllerForPresentingModalView queue:mainQueue usingBlock:
^(NSNotification *note) {
[_bannerView setAutoReloadEnabled:NO];
}];
Which didn't work either, so I ended up using another method from NSNotificationCenter to avoid the block, but still I don't get why the __block was retaining the object, Could somebody clarify this for me? Do i have a wrong concept of __block?
Upvotes: 0
Views: 51
Reputation: 33421
It won't retain the object in a non-ARC environment, but it will in an ARC environment. For ARC, use __weak
instead of __block
.
Source: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#blocks
Upvotes: 3