Re: __block __weak - am I doing this right?
Re: __block __weak - am I doing this right?
- Subject: Re: __block __weak - am I doing this right?
- From: Matt Neuburg <email@hidden>
- Date: Thu, 16 Feb 2012 12:57:55 -0800
On Feb 16, 2012, at 12:45 PM, Greg Parker wrote:
> On Feb 16, 2012, at 12:42 PM, Matt Neuburg <email@hidden> wrote:
>> On Feb 16, 2012, at 12:05 PM, Greg Parker wrote:
>>>
>>> The question is, who retains the observer? The __block __weak variable does not, because it's weak. NSNotificationCenter does not, as I understand it.
>>
>> It does, actually; thanks for pressing me on this point.
>
> The NSNotificationCenter documentation says:
> "Important: The notification center does not retain its observers…"
We may be talking at cross purposes.
If you register by saying addObserver:selector:name:object:, the notification center does not retain the observer object named in the first param.
But if you register by saying addObserverForName:object:queue:usingBlock:, it returns an observer object, and the notification center *does* retain that observer. And, since that block will probably mention self, and since the observer effectively consists of a wrapper for that block, the observer is retaining you. Hence the cycle. You *must* unregister or you will leak yourself. The example I cited shows how to do that.
m.
--
matt neuburg, phd = email@hidden, http://www.apeth.net/matt/
pantes anthropoi tou eidenai oregontai phusei
Programming iOS 5! http://shop.oreilly.com/product/0636920023562.do
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden