Re: XML Resource Release
Re: XML Resource Release
- Subject: Re: XML Resource Release
- From: Jeffrey Walton <email@hidden>
- Date: Thu, 19 May 2011 04:23:37 -0400
On Wed, May 18, 2011 at 2:16 PM, Ken Thomases <email@hidden> wrote:
> On May 18, 2011, at 12:55 PM, Bing Li wrote:
>
>> NSXMLElement *root = [NSXMLNode elementWithName:"MessageRoot"];
>
> As per Cocoa's memory management conventions, you don't not own the object returned by -[NSXMLNode elementWithName:]. You have not invoked a method whose name contains "alloc", "new", or "copy" or which is explicitly documented as giving its caller ownership rights and responsibilities.
>
>> NSXMLElement *peerKeyElement = [NSXMLNode elementWithName:"PeerKey"];
>
> Same here.
>
>> NSXMLElement *peerNameElement = [NSXMLNode elementWithName:"PeerName"];
>
> Same here.
>
>> NSXMLElement *passwordElement = [NSXMLNode elementWithName:"Password"];
>
> Same here.
>
>> NSData *data = [xmlDoc XMLDataWithOptions:NSXMLNodePrettyPrint];
>
> Same here.
>
>> [passwordElement release];
>> // [peerNameElement release];
>> // [peerKeyElement release];
>> [root release];
>> [xmlDoc release];
>> [xmlStr release];
>> [data release];
>
> Many of these releases are wrong, not just the ones you have commented out.
> Of these, you only own xmlDoc and xmlStr, so those are the only ones you are
> entitled to release. If you didn't happen to get exceptions from releasing the
> others, it was an unhappy accident. (Unhappy because it hid your bug. It is
> always better for bugs to be found early.)
If it helps Bing (and coming from someone who frequently needs the
same sort of help): (1) Run you code using Instruments, or (2) select
Executable -> Arguments and add NSZombieEnabled = YES. Both should
help you flush out the problems early (as Ken suggests).
> Please review the Memory Management Programming Guide <http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/>. It explains all of this.
>
Also grab a book or two. It will help you digest it. Nuremberg's book
would be a good choice since the doctor stays active on this list.
Jeff
_______________________________________________
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