Re: Why Wasn't Memory Collected?
Re: Why Wasn't Memory Collected?
- Subject: Re: Why Wasn't Memory Collected?
- From: "Stephen J. Butler" <email@hidden>
- Date: Sun, 12 Jun 2011 00:45:51 -0500
On Sat, Jun 11, 2011 at 1:03 PM, Bing Li <email@hidden> wrote:
> NSData *data = [xmlDoc XMLData];
> NSString *xmlStr = [[[NSString alloc] initWithData:data
> encoding:NSUTF8StringEncoding] autorelease];
> xmlStr = [xmlStr stringByAppendingString:@"\n"];
> const char *xmlChar = [xmlStr UTF8String];
>
> [xmlDoc release];
> return xmlChar;
> }
If you're using your own autorelease pools now, be careful about the
lifetime of your return value (xmlChar)! It will only live as long as
xmlStr lives. For example, this would be incorrect:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
const char *str = [self createSendMessage:...];
[pool release];
// do something with str
You'll want to strdup() the return value to make sure it lives after
the pool is released, but then you also need to remember to free()
your strdup() value.
_______________________________________________
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