• 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
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]

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


  • Subject: What is the life of the c string returned by NSString's UTF8String method?
  • From: Stuart Malin <email@hidden>
  • Date: Fri, 18 Sep 2009 14:04:27 -0400

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?

I have a class with some (statically allocated) class variables. In the class's -initialize method, I create a retained NSString. I set one of the class variables to the value returned by -UTF8String (as sent to that retained NSString). I use this char* value in some C calls made from instance methods of the class.

The first time an object of the class is instantiated, this works fine. But it seems that for subsequent instances, although the value of the char* pointer remains unchanged, the memory it is pointing to is changed (i.e., no longer a C string representation of the NSString). So, either the NSString has done something with the memory pointed to by the initialized reference (i.e., I can't hold onto the reference as I have been), or somehow the memory is getting corrupt in some other way. I will look into the latter but only if my assumption about the former is correct.

TIA.


_______________________________________________

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: Nick Zitzmann <email@hidden>
    • Re: What is the life of the c string returned by NSString's UTF8String method?
      • From: Jens Alfke <email@hidden>
    • Re: What is the life of the c string returned by NSString's UTF8String method?
      • From: "Sean McBride" <email@hidden>
    • Re: What is the life of the c string returned by NSString's UTF8String method?
      • From: Jesper Storm Bache <email@hidden>
  • Prev by Date: Re: Populating TableView Via Button.
  • Next by Date: Re: Menu shortcuts without modifiers?
  • Previous by thread: Re: 2 simple questions
  • Next by thread: Re: What is the life of the c string returned by NSString's UTF8String method?
  • Index(es):
    • Date
    • Thread