Re: XML Resource Release
Re: XML Resource Release
- Subject: Re: XML Resource Release
- From: "Hank Heijink (Mailinglists)" <email@hidden>
- Date: Wed, 18 May 2011 14:10:45 -0400
On May 18, 2011, at 1:55 PM, Bing Li wrote:
> Dear all,
>
> I am creating an XML chars to transmit over TCP. When an XML is created, I
> attempt to release some resources. However, I got some weird results.
>
> The XML is simple as follows.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <MessageRoot>
> <PeerKey>2412432</PeerKey>
> <PeerName>greatfree</PeerName>
> <Password>111111</Password>
> </MessageRoot>
>
> The code to create the above XML is as follows. It runs well.
>
> + (const char *) CreateSignInMessage: (NSString *)peerKey PN: (NSString
> *)peerName PW:(NSString *)password
> {
> NSXMLElement *root = [NSXMLNode elementWithName:"MessageRoot"];
> NSXMLDocument *xmlDoc = [[NSXMLDocument alloc]
> initWithRootElement:root];
> [xmlDoc setVersion:"1.0"];
> [xmlDoc setCharacterEncoding:"utf-8"];
>
> NSXMLElement *peerKeyElement = [NSXMLNode
> elementWithName:"PeerKey"];
> [root addChild:peerKeyElement];
> [peerKeyElement addChild:[NSXMLNode textWithStringValue:peerKey]];
>
> NSXMLElement *peerNameElement = [NSXMLNode
> elementWithName:"PeerName"];
> [root addChild:peerNameElement];
> [peerNameElement addChild:[NSXMLNode textWithStringValue:peerName]];
>
> NSXMLElement *passwordElement = [NSXMLNode
> elementWithName:"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];
> // [peerNameElement release];
> // [peerKeyElement release];
> [root release];
> [xmlDoc release];
> [xmlStr release];
> [data release];
You don't own passwordElement, root, or data. Expect crashes when you release those. You might want to read up on the memory management guidelines:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
Best,
Hank
_______________________________________________
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