1.Reference Counter
Each language has its own memory management mechanism, and of course OC is no exception. When an object is created, the system allocates a storage area to the object in the heap. This object is pointed by the object pointer in the stack. When no pointer points to this object, how does the system release the object memory? The reference counter is used in OC. Each object has a reference counter (four bytes). When an object is created, its reference counter value is 1. When a retain message is sent to an object, the Reference Counter of the object performs the + 1 operation, indicating that a pointer owns the object. When a release message is sent, the reference counter performs the-1 operation, indicates that a pointer does not own this object. When the reference counter of an object is 0, it indicates that no pointer has the object, and the system will automatically call the dealloc method to destroy the object. In general, it is not much different from the management mechanism in C language, but there are some more method calls.
The following code demonstrates how to create and operate a person class.
1 #import <Foundation/Foundation.h>2 3 @interface Person : NSObject4 5 @property int age;6 7 @end
1 # import "person. H "2 3 @ implementation person 4 5 // rewrite the dealloc method. When a person object is recycled, this method 6-(void) is automatically called) dealloc 7 {8 nslog (@ "person object recycled"); 9 10 // The dealloc of Super must be called and placed at the end of 11 [Super dealloc]; 12} 13 14 @ end
# Import <Foundation/Foundation. h> # import "person. H "int main () {// when the object is created, the reference counter is 1 person * P = [person alloc] init]; // obtain the reference counter nsuinteger c = [p retaincount]; nslog (@ "counter: % lD", c); // The retain method is called, the counter is changed to 2 person * P2 = [p retain]; // The release method is called, and the counter is changed to 1 [P release]; // The release method is called, the counter is changed to 0. At this time, the system calls the dealloc method to release the memory [P release]; return 0;
Note: The retain method returns the object itself because the method itself is a copy object, for example, in the above Code, person * P2 = [p retain] is a pointer to the person object address allocated in the stack, that is, a pointer to the person object, at this time, the Reference Counter of the person object must be + 1 to mark whether a pointer has me.
2.Wild pointer, empty pointer, And zombie object.
1> zombie objects: the occupied memory has been recycled. Zombie objects cannot be used any more.
2> wild pointer: pointer to a zombie object (memory unavailable). An error is returned when a message is sent to the wild pointer (exc_bad_access)
3> NULL pointer: no pointer pointing to anything (nil, null, and 0 are stored). No error is returned when messages are sent to null pointers.
In some cases, botnets and wild pointers may exist.
Dark Horse programmer-reference counter for Memory Management