Re: memory deallocation
Re: memory deallocation
- Subject: Re: memory deallocation
- From: Andrew Farmer <email@hidden>
- Date: Sat, 4 Apr 2009 21:59:31 -0700
On 04 Apr 09, at 21:28, Michael Ash wrote:
On Sat, Apr 4, 2009 at 6:02 PM, Andrew Farmer <email@hidden>
wrote:
In your case, if you're trying to create a NSData object with the
contents
of a string, the correct usage is:
char *cstring = "some text";
return [NSData dataWithBytesNoCopy:cstring length:strlen(cstring)];
We use dataWithBytesNoCopy here because the contents of the string
are in
constant data, and can be guaranteed to not change or go away.
This is no good. NSData will attempt to free your constant string when
it's destroyed. You need to explicitly say freeWhenDone:NO here,
because not specifying it is equivalent to YES.
Erp... right you are. That last line should indeed be
return [NSData dataWithBytesNoCopy:cstring length:strlen(cstring)
freeWhenDone:NO];
_______________________________________________
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