Re: Cocoa class extension best practice
Re: Cocoa class extension best practice
- Subject: Re: Cocoa class extension best practice
- From: Charles Srstka <email@hidden>
- Date: Wed, 16 Oct 2013 16:39:04 -0500
On Oct 16, 2013, at 4:20 PM, Steve Mills <email@hidden> wrote:
> On Oct 16, 2013, at 12:56:00, Andy Lee <email@hidden> wrote:
>
>> As for the creeping memory footprint, maybe try Heapshot Analysis to see what objects are being created during particular intervals?
>
> Aha! Thanks, I didn't know about this very helpful debugging aid yet. I was getting about a meg worth of junk every time I brought up a multi-panel dialog that loads 26 nibs, with most of the objects rooted in a call to instantiateNibWithOwner. Looking that method up, I see it's been deprecated and replaced by instantiateWithOwner in 10.8. I tried that instead and now it's only growing by a few measly k every time. This is *much* better. But, we still need to support 10.7. So it looks like we'll just have to leak all this crap in 10.7 and run the newer method on 10.8 and up.
Aha, that definitely explains the leaks you've been getting. The trouble with instantiateWithOwner:, and the reason it's been deprecated, is because it doesn't follow the standard Cocoa memory management rules. Specifically, it retains the top-level objects, and leaves it your responsibility to release them, even though you never alloced, copied them, or retained them. There are three ways to deal with this:
1. Have an outlet to each top-level object in the nib, and make sure to release them all when your object deallocates.
2. Store the array returned as the second parameter of -instantiateWithOwner:topLevelObjects:, and when your object deallocates, loop through the array and release everything.
3. (This is the one I'd recommend) Instead of using NSNib, make a subclass of NSWindowController (if your nib defines a window) or NSViewController (if your nib defines a view). Then use either -[NSWindowController initWithWindowNibName:] or -[NSViewController initWithNibName:bundle:] to load the nib. This will produce correct behavior on any version of OS X, including 10.7, and gives you a few other niceties as well.
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