Re: XML Resource Release
Re: XML Resource Release
- Subject: Re: XML Resource Release
- From: Bing Li <email@hidden>
- Date: Fri, 20 May 2011 22:43:56 +0800
Dear Jeff and all,
I appreciate so much for your help! After reading the document from
apple.com, I changed my code as follows. It must be fine.
Best regards,
Bing
+ (const char *) CreateSignInMessage: (NSString *)peerKey PN: (NSString
*)peerName PW:(NSString *)password
{
NSXMLElement *root = [NSXMLNode
elementWithName:MessageConstants.XML.MESSAGE_ROOT];
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc]
initWithRootElement:root];
[xmlDoc setVersion:MessageConstants.XML.XML_VERSION];
[xmlDoc setCharacterEncoding:MessageConstants.XML.XML_ENCODING];
NSXMLElement *peerKeyElement = [NSXMLNode
elementWithName:MessageConstants.XML.PEERKEY];
[root addChild:peerKeyElement];
[peerKeyElement addChild:[NSXMLNode textWithStringValue:peerKey]];
NSXMLElement *peerNameElement = [NSXMLNode
elementWithName:MessageConstants.XML.PEERNAME];
[root addChild:peerNameElement];
[peerNameElement addChild:[NSXMLNode textWithStringValue:peerName]];
NSXMLElement *passwordElement = [NSXMLNode
elementWithName:MessageConstants.XML.PASSWORD];
[root addChild:passwordElement];
[passwordElement addChild:[NSXMLNode textWithStringValue:password]];
NSData *data = [xmlDoc XMLDataWithOptions:NSXMLNodePrettyPrint];
NSString *xmlStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
const char *xmlChar = [xmlStr UTF8String];
/*
[passwordElement release];
[root release];
[xmlStr release];
[data release];
*/
[xmlDoc release];
[xmlStr autorelease];
return xmlChar;
}
On Thu, May 19, 2011 at 4:23 PM, Jeffrey Walton <email@hidden> wrote:
> 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