• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: memory deallocation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >memory deallocation (From: Daniel Luis dos Santos <email@hidden>)
 >Re: memory deallocation (From: Andrew Farmer <email@hidden>)
 >Re: memory deallocation (From: Michael Ash <email@hidden>)

  • Prev by Date: [Moderator] Re: Toll-free bridge type at runtime
  • Next by Date: Re: Obj-C equivalent to Python generator functions
  • Previous by thread: Re: memory deallocation
  • Next by thread: Re: memory deallocation
  • Index(es):
    • Date
    • Thread