Question

What is the result of the following?

NSString *myStr = [[[NSString alloc] initWithString:@"Hello World."] autorelease];
myStr = [NSString stringWithString:@"Hello Again."];

Does myStr get correctly released or does this crash, since we would call autorelease on myStr which is now set to a string that is already autoreleased?

Was it helpful?

Solution

Your code example works the way you would expect. autorelease can't somehow change what object it refers to after you send the message. The @"Hello World." and @"Hello Again." objects are different objects, even though your example uses the same pointer variable to refer to them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top