Re: Convience Methods and Collections
Re: Convience Methods and Collections
- Subject: Re: Convience Methods and Collections
- From: Ryan Marsh <email@hidden>
- Date: Fri, 30 Mar 2007 13:30:57 -0500
Thanks to all who responded.
Yes rootNode is a dictionary. Thanks for your help, it must be that
something else is wrong. I'll look in to NSZombie and ObjectAlloc.
The only difference between it working and not working is when I use
initWith... vs. arrayWith. My gut tells me (having limited knowledge
of Objective-C) that using the alloc and initWith would be an over-
retain when adding to a collection. But faced with the results of
this test I was confused... and still am.
Like you said, it must be something else.
Thanks again,
-ryan
On Mar 30, 2007, at 10:32 AM, James Bucanek wrote:
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