It is well known that memory management in Objective-c is managed through a mechanism called "reference counters".
For example, when I declare a new instance
NSData *data = [[NSData alloc] init]
Now, there is an object of type NSData in memory called data. The value of its reference counter is 1. If I refer to it again, the value of his reference count will be +1 to 2.
Call when you are finished using
[Data release];
Make the reference count-1. When the value is zero, the system reclaims the data instance and frees the memory.
But just as I saw an article on Cocoachina, the author asked if it would cause a memory leak in this case? Why not crash?
The code is as follows:
NSString *str = [[NSString alloc] initwithstring:@ "ABC"];
str = @ "123";
[STR release];
NSLog (@ "%@". Str);
First, let's analyze this piece of code first.
The first sentence declares an instance of the NSString type STR, and initializes it with a value of @ "ABC" after Init
On the second line, the pointer to str points to a constant @ "123". In theory, the "ABC" initialized at the first line does not have any pointers pointing. So it caused a memory leak.
Then the third line, the reference count of STR-1
The value of the four-row output STR is 123.
First answer why not crash, because the third line of release is actually release a constant @ "123" and as a constant, its default reference meter value is very large (100k+)
If you don't believe me, you can try this.
NSLog (@ "Retaincount =%d", [@ "123" retaincount]);
The final output value will be a very large number. So a single release is not going to let it out.
Then answer this will not cause a memory leak.
In fact............ Theoretically speaking!
But in fact, OBJECTIVE-C has special care for the NSString type. The default initial values for all NSString reference counters are very, very large.
NSString is an immutable string object. This is not to say that the value of the variable declared by this object is immutable, but rather that after it is initialized, you cannot change the value in memory allocated by the variable, but you can reassign the memory space in which the variable is located.
There are three ways to generate a string of type NSString:
Method 1. Direct assignment: NSString *STR1 = @ "my string";
Method 2: Initialization of the class function is generated by nsstring *STR2 = [NSString stringwithstring:@ "my string"];
Method 3. Instance method initialization generation: NSString *STR3 = [[NSString alloc] initwithstring:@ "my string"];
NSString *STR4 = [[NSString alloc]initwithformat:@ "my string"];
Difference 1: Method one generates a string, the memory space is not initialized, so the memory is not freed after the end of the use;
And the other three will initialize the memory space, the use of the end to free memory;
Methods 2 and 3 are also different when memory is freed, Method 2 is the Autorelease type, memory is released by the system, and Method 3 must be released manually
Difference 2: A string initialized with format, it is necessary to initialize a dynamic memory space, such as: 0X6A42A40;
String-declared strings, which initialize the constant memory area, such as: 0X46A8, the address of the constant memory area, as long as the values are the same, occupy the address space is consistent.
So the addresses of STR3 and str1 are the same, but the addresses of STR4 and str1 are inconsistent.
You can test it yourself.
If the example is not nsstring but other types such as NSData, NSNumber, etc...
Will definitely cause a memory leak.
The consequences of a memory leak will not be apparent for a short period of time. But in the long run, the consequences are very, very serious.
Students must avoid the occurrence of memory leaks.
If you want to do this, you have to develop a good coding habit.
Quote official documents in a sentence saying that
Who quoted who was responsible for releasing
It's just who's init, Retain, copy. Who is in charge of release.
In theory, the number of init+retain+copy in your code should be equal to the number of release.
This is the best way to avoid memory leaks.
NSString is not managed by the reference counter mechanism in OBJECTIVE-C