Re: ARC dealloc best pratice
Re: ARC dealloc best pratice
- Subject: Re: ARC dealloc best pratice
- From: Jens Alfke <email@hidden>
- Date: Fri, 06 Feb 2015 09:34:55 -0800
> On Feb 6, 2015, at 6:48 AM, Jonathan Mitchell <email@hidden> wrote:
>
> // remove observers
> // unregister for notifications
I have to confess I'm still not completely certain whether these are needed under ARC. I remember reading something about at least one of these being handled automatically, but I just skimmed through some docs now and couldn't find anything. I tend to put these in my classes but always wonder whether I strictly need to.
> // set any non-weak delegates to nil (NSTableView et al)
I think you meant "set any _weak_ delegates to nil", i.e. clear the delegate property of any object of whom I'm the delegate. Non-weak doesn't make sense, because if a delegate has a strong reference to self, then self can't possibly be in its dealloc method.
Note that this is only necessary if (a) the other object can outlive self, and (b) the other object could still send the delegate messages in the future.
> // invalidate any timers
This is incorrect, because NSTimer's target property is a strong delegate. So if self is currently the target of any NSTimer, it couldn't be deallocated. In fact, if you're in a dealloc method and still have a reference to such a timer, it indicates there's a refcounting error in your code! Worse, invalidating it in -dealloc will certainly cause a crash because it'll call -release on you during your dealloc…
—Jens
_______________________________________________
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