Re: Cocoa class extension best practice
Re: Cocoa class extension best practice
- Subject: Re: Cocoa class extension best practice
- From: Jens Alfke <email@hidden>
- Date: Tue, 15 Oct 2013 13:32:20 -0700
On Oct 15, 2013, at 12:50 PM, Steve Mills <email@hidden> wrote:
> Instruments shows that we're leaking NSArrays created when itemArray returns. I assume whoever wrote these methods was assuming that itemArray would simply return the internal NSArray and not make a copy.
You don’t need to release the return value of -itemArray. It’s a public method, and by the standard Cocoa convention it returns an autoreleased result (since its name doesn’t start with ‘alloc’, ‘new’ or ‘copy’.)
> NSArray* items = [self itemArray];
> for(NSMenuItem* item in items)
> blah;
> [items release];
That’s going to crash by over-releasing ‘items’. _Unless_ something in the “blah” code is accidentally retaining the array. But in that case you need to fix the “blah” code itself, not tack on an extra -release at the end of the loop as a band-aid for the leaking.
Have you run the Clang static analyzer on your code? It’s very good at finding these sorts of problems.
—Jens
_______________________________________________
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