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

  • Home
  • SEARCH
  • 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 8039521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:35:51+00:00 2026-06-05T03:35:51+00:00

I have a problem with this code : __strong NSString *yourString = @Your String;

  • 0

I have a problem with this code :

__strong NSString *yourString = @"Your String"; 
__weak NSString *myString = yourString;
yourString = nil;
__unsafe_unretained NSString *theirString = myString;
NSLog(@"%p %@", yourString, yourString);
NSLog(@"%p %@", myString, myString);
NSLog(@"%p %@", theirString, theirString);

I’m expecting all pointers to be nil at this time, but they are not and I don’t understand why.
The first (strong) pointer is nil but the other two are not. Why is that?

  • 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-06-05T03:35:52+00:00Added an answer on June 5, 2026 at 3:35 am

    tl; dr: The problem is that the string literal never gets released so your weak pointer still points to it.


    Theory

    Strong variables will retain the value they point to.

    Weak variables won’t retain their value and when the value is deallocated they will set their pointer to nil (to be safe).

    Unsafe unretained values (as you probably can read by the name) won’t retain the value and if it gets deallocated they do nothing about it, potentially pointing to a bad piece of memory


    Literals and constants

    When you create a string using @"literal string" it becomes a string literal that never will change. If you use the same string in many places in your application, it is always the same object. String literals doesn’t go away. Using [[NSString alloc] initWithString:@"literal string"] won’t make a difference. Since it becomes a pointer to the literal string. It is however worth noting that [[NSString alloc] initWithFormat:@"literal string"]; works differently and will the release its string object.

    Line by line:

    __strong NSString *yourString = @"Your String"; 
    

    You are creating a strong pointer to a string. This will ensure that the value does not go away. In your case it’s a little bit special since the string is a string literal that technically won’t be released.

    __weak NSString *myString = yourString;
    

    You create a weak pointer to the same thing as your strong pointer. If at this time the strong pointer would point to something else, the value it is pointing to would get deallocated, then the weak pointer would change its value so that it points to nil. Now it still points to the same as the strong pointer.

    yourString = nil;
    

    Your strong pointer points to nil. Nothing points to the old string so it should get released if it wasn’t for the fact that is was a literal string. If you tried the exact same thing with other objects that you created yourself, the weak variable would change so that it points to nil. But, since the string literal is literal and doesn’t go away. The weak variable will still point to it.

    __unsafe_unretained NSString *theirString = myString;
    

    A new unretained pointer is created, pointing to your weak pointer which is pointing to the string literal.

    NSLog(@"%p %@", yourString, yourString);
    NSLog(@"%p %@", myString, myString);
    NSLog(@"%p %@", theirString, theirString);
    

    You print all your strings and get confused why the first value is nil but the other two aren’t.


    Related reading:

    What’s the difference between a string constant and a string literal?

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

Sidebar

Related Questions

I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
Hi i have problem with this code, i found it on the internet and
i am a beginner and i have a problem : this code doesnt compile
I have a problem with this code right here: - (void)fetchedData:(NSData *)responseData { //parse
I'm a beginner with jdbc ... I have a problem running this code :
I have a problem with this is code: Set oXmlHTTP = CreateObject(Microsoft.XMLHTTP) oXmlHTTP.Open POST,
I have a problem here with this code. I'm opening a socket, then listening
Im create this code to store page view in database, but i have problem
I have this code, but I have a problem. When I update but do
I have a problem with the logic of this code, XNA's Update Method is

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.