Re: XML Resource Release
Re: XML Resource Release
- Subject: Re: XML Resource Release
- From: Ken Thomases <email@hidden>
- Date: Wed, 18 May 2011 13:16:28 -0500
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.)
Please review the Memory Management Programming Guide <http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/>. It explains all of this.
Regards,
Ken
_______________________________________________
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