Common memory management errors
The memory is not automatically managed.
Solution:
Wild pointer Problems
[Person retaincount]: Message sent to deallocated instance 0x1002032d0 message sent to a recycled object
Abnormal wild pointer: It may crash or crash. It suddenly crashes when writing a line of code (No Code related to reference count is written) cause: space of the object, the object has been recycled by the system and cannot be accessed. Solution: After the space is reclaimed by the system, access is prohibited or directed to an invalid space.
Excessive release
Excessive release: The program crashes immediately after the memory reference count minus one operation is called. cause: After the space is recycled by the system, you cannot perform operations related to the reduction of the reference count by one. Otherwise, the system will crash immediately. Solution: delete redundant release statements.
When the reference count is zero, the system automatically recycles the memory. We only manage the reference count.
@ Autoreleasepool usage principle @ autoreleasepool {person * person = [[person alloc] init]; // 0-1
[Person retain]; // 1-2 nslog (@ "% lD", [person retaincount]); @ autoreleasepool {[person autorelease]; // 2-1} autorelease, the object declared as autorelease is put into the automatic release pool closest to it. When the automatic release pool is destroyed, a release message is sent to each object in the pool.
When the reference count of this type of object is zero, the system will automatically call the dealloc method of this class to reclaim space. This method is automatically called by the system and cannot be manually called to verify the object, whether the space has not been recycled. You only need to check whether the dealloc method of the class has been executed, as shown in the following-(void) dealloc {nslog (@ "dealloc, you are finished, the space is recycled "); [Super dealloc];}
Basic Principle of Memory Management: if you have the ownership of an object after alloc retain copy, you must perform release or autorelease on it. (If alloc retain copy is called, the release or
Autorelease)
Typical examples of Memory Management
1. In the method of an object, assume that the semantic feature of name is retain.
Self. Name= [[Nsstring alloc] initwithformat: @ "object"];
And
_ Name = [[nsstring alloc] initwithformat: @ "object"];
Is there any difference?
Self. Name: assign a value to the name first. The setter method is called. The reference count + 1 operation is performed in the setter method. Therefore, the reference count of the setter method is 2.
_ Name is only a simple value assignment operation, so the reference count does not change
2.-(void) setpeople :( nsstring *) P {
Self. People = P;
}
What is the problem with this code.
There will be an endless loop, self. People will call their own setter, and self. people is still inside the setter method, so there will be an endless loop
3. There is an nsstirng type. What is the role of each line of code in the setter method of the name attribute declared by retain?
-(Void) setname :( nsstring *) name {
Determine whether the original object and the new object are the same object. If it is the same, there is no need to re-assign values. Otherwise, it will be release first and then retain, and it will become a wild pointer.
If (_ name! = Name) {release the ownership of the previous object [_ namerelease]; keep the instance variable _ name with the ownership of the new object _ name = [nameretain];}
Common memory management errors