• 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: What is the life of the c string returned by NSString's UTF8String method?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What is the life of the c string returned by NSString's UTF8String method?


  • Subject: Re: What is the life of the c string returned by NSString's UTF8String method?
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Fri, 18 Sep 2009 22:40:37 +0200


Le 18 sept. 2009 à 20:20, Nick Zitzmann a écrit :


On Sep 18, 2009, at 12:04 PM, Stuart Malin wrote:

I am under the impression that the reference returned by NSString's UTF8String method is valid for the life of the NSString instance which provided the reference (and further, that the memory of the referenced C string is freed when the NSString is released). Is this correct?

The only way to be sure is to run Instruments with the object allocations tool and look for allocations equal to the size of the string. But I'm pretty sure that draining the autorelease pool frees the C strings, or at least, they do get cleaned up at some point.


In any case, if you want them to stick around, then you need to memcpy() them into a data buffer that is controlled by your application.


Or to avoid a copy and raw memory management, you can also query directly an NSData from the string using -[NSString dataUsingEncoding:NSUTF8StringEncoding] and then use -[NSData bytes] as the returned value for this method is guarantee to have the same life as the NSData object.


-- Jean-Daniel




_______________________________________________

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


  • Follow-Ups:
    • Re: What is the life of the c string returned by NSString's UTF8String method?
      • From: Wade Tregaskis <email@hidden>
References: 
 >What is the life of the c string returned by NSString's UTF8String method? (From: Stuart Malin <email@hidden>)
 >Re: What is the life of the c string returned by NSString's UTF8String method? (From: Nick Zitzmann <email@hidden>)

  • Prev by Date: Re: Crash on SL in com.apple.DesktopServices after using NSOpenPanel
  • Next by Date: getting accessor method info in a different class.
  • Previous by thread: Re: What is the life of the c string returned by NSString's UTF8String method?
  • Next by thread: Re: What is the life of the c string returned by NSString's UTF8String method?
  • Index(es):
    • Date
    • Thread