Weak NSString variable is not nil after setting the only strong reference to nil

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?

Leave a Comment