Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Convience Methods and Collections



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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: Convience Methods and Collections (From: James Bucanek <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.