I was going through the memory management concepts. I created one string1 and assign that string1 into another string2, now I release this string1.
Here string2 retain count is 1 but on NSLog statement it gives EXC Bad access.
When I am assigning the string
NSString * string1 = [[NSString alloc]initWithFormat:@"hello"];
string2 = string1;
NSLog(@"string1 memory address = %p, string2 memory address = %p", &string1, &string2);
[string1 release];
NSLog(@"[string2 retainCount] = %lu", (unsigned long)[string2 retainCount]);
NSLog(@"string2 = %@", string2); // here app is crashing
Does it means that string2 has an autorelease message also with it because if I do string2 = [string1 copy]; instead of string2 = string1; it doesn't crash.
So I wanted to ask whether the crash is because it has autorelease message of string2 and how it is relating with string2 release command.
Please advice!
Assignment doesn't change object's retain count if you use manual memory management in Objective-C. And you for sure use it, otherwise, you can't invoke
releasemethod in your code.So, your code does the following. It creates
NSStringobject with retain count = 1, and assigns it tostring1pointer. After that, you assignsstring1tostring2. Now you have 2 pointers to the same object, and retain count of this object is still 1. Then you release object, it deallocated immediately. And after that you experiencing crash:To fix that, you can use
retainwhen you do an assignment.Also, you can use
copy. Note that forNSStringcopy doesn't actually copies an object, it simply invokesretain. There is no need to perform actual copying, becauseNSStringis immutable and can't be changed. If we will useNSMutableString, things will change:Alternatively, you can use ARC. It will insert corresponding retain/release calls at compile time. Code then will look like:
I suggest to understand manual memory management first, and after that migrate to ARC.