Re: Convience Methods and Collections
Re: Convience Methods and Collections
- Subject: Re: Convience Methods and Collections
- From: James Bucanek <email@hidden>
- Date: Fri, 30 Mar 2007 08:32:25 -0700
Ryan Marsh wrote on Friday, March 30, 2007:
>I apologize in advance for such a newbie question.
>Why do objects created with convenience methods get auto-released
>when I add them to a collection?
They don't. Objects get retained when added to a collection and released when removed.
>This simple example from the init of an NSOutlineView datasource:
>
>[rootNode setObject:[NSArray
>arrayWithObjects:devices,folders,playlists,nil] forKey:@"children"];
Is rootNode a dictionary? If so, then it should retain the NSArray. If it's not, then NSArray will be autoreleased.
>
> The array will be released before the first time I call
>
>[rootNode objectForKey:@"children"]
Something else is releasing the array, or possibly the dictionary itself, or rootNode isn't retaining it.
NSZombie and ObjectAlloc are your friends.
Start by turning on NSZombies. This will catch any messages sent to objects that have been released. Another tool is OjectAlloc, which is a program that will trace all of the allocs, retains, autoreleases, and releases that sent to an object. Create a short test the demonstrates the problem, run the app using ObjectAlloc, and see where the NSArray is getting released.
Debugging this might be a bit of a challenge, because NSDictionary and NSArray objects are quite common. It's much easier to isolate a problem like this for your own classes because it's easy to isolate that activity from the rest of what's going on. But the principle is the same either way.
>If I use [[NSArray alloc]
>initWithObjects:devices,folders,playlists,nil] then it works fine.
This code would overretain the object, since the collection retains it too. Something else is wrong.
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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