Re: Referencing an object after sending a release message to it
Re: Referencing an object after sending a release message to it
- Subject: Re: Referencing an object after sending a release message to it
- From: Charles Srstka <email@hidden>
- Date: Sun, 07 Aug 2011 15:34:25 -0500
On Aug 6, 2011, at 6:57 PM, Thomas Wetmore wrote:
> It may look odd to use an object that has been just released, but there is nothing wrong in this case, and no assumptions are needed here. As soon as the object has been added to the array it has been safely retained on the same thread by well-understood and well-documented rules, and it should be released as soon thereafter as is practical. I too don't like the way this code feels, but moving the release to the end has no effect other than making some of us possibly feel good. For those who feel best when memory is released as soon as it is possible to do so, this code is fine. For however laudable it is to write comfortable code one must decide for oneself where to put the release.
I disagree. It may *work* in this case, but that is because of the implementation detail that employeeController puts the same object into its array instead of copying it, and then on the implementation detail that NSArray retains its contents instead of copying them, and the implementation detail that employeeController isn’t using a custom non-retaining CFArray or something. Any of that stuff changes, you get a crash.
There’s no real reason to release the object before you’re done with it, so just don’t do that. Keep the object while you need it, and then release it after you’re done with it.
Charles_______________________________________________
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