Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 866681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:50:57+00:00 2026-05-15T09:50:57+00:00

The docs vaguely say that it will stick around as long as needed, but

  • 0

The docs vaguely say that it will stick around as long as needed, but when is it actually deallocated? When the original NSString is deallocated? When the current autorelease pool clears? Sometime else? Furthermore, when is it guaranteed to stay for, as opposed to the current implementation? If it stays for the lifetime of the NSString, will the same pointer be returned every time?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:50:57+00:00Added an answer on May 15, 2026 at 9:50 am

    Update 30/11/2018

    As David Dunham pointed out in comments, the Apple documentation has changed (the old link in my answer is now broken).

    The Apple documentation now says:

    This C string is a pointer to a structure inside the string object, which may have a lifetime shorter than the string object and will certainly not have a longer lifetime. Therefore, you should copy the C string if it needs to be stored outside of the memory context in which you use this property.

    My original answer is now wrong in two parts.

    My claim that the UTF8 string could possibly survive the deallocation of the NSString is explicitly denied by the current Apple documentation.

    In the Apple docs “Autorelease context” has been replaced by “memory context”. I’m not sure what that means exactly, but you probably should not trust the pointer after you have exited the scope in which it was acquired.

    The Original Answer

    The Apple documentation (broken link) says:

    The returned C string is automatically freed just as a returned object would be released; you should copy the C string if it needs to store it outside of the autorelease context in which the C string is created.

    That seems pretty non vague to me.

    NB You shouldn’t really worry about what the current implementation does. Implementations change

    Extrapolating the above, the answers to your questions are probably:

    but when is it actually deallocated?

    When the autorelease pool is drained.

    When the original NSString is deallocated?

    No. The documentation implies you could alloc a string, take the UTF8String then release the string and the UTF8String would still be valid but be aware of this line from the Memory Management Rules:

    A received object is normally guaranteed to remain valid within the method it was received … although you must also take care if you modify an object from which you received another object.

    “Modifying an object” might include releasing the NSString you got the UTF8 string from.

    When the current autorelease pool clears?

    That’s what the docs say.

    Sometime else?

    No.

    Furthermore, when is it guaranteed to stay for, as opposed to the current implementation?

    If it’s not documented, it’s not guaranteed.

    If it stays for the lifetime of the NSString, will the same pointer be returned every time?

    I doubt it. Maybe for immutable strings, the UTF8 string is cached, but maybe not. Why not write some code to try it out? In any case, you can’t rely on it in production code, nor in any situation where your NSString might really be an NSMutableString.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MSDN docs say that only value types need boxing, but this does not apply
The docs say that a class is hashable as long as it defines __hash__
Python docs states that uuid1 uses current time to form the uuid value. But
The docs about openssl_sign() say that the signature is returned in the passed string
The docs say that I should not release any modeled property in -dealloc. For
Django docs say that Context object is a stack: from django.template import Context c
The docs say that calling CTTypesetterCreateLine is the same as calling CTTypesetterCreateLineWithOffset with offset
The docs mention :XML, :JSON, and :HTML, but I have a URL that returns
The docs says that ancestor will apply a filter of given ancestor being somewhere
The docs say: + (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize The fully specified name of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.