Objective-C Knowledge Summary (3), objective-c Knowledge Summary
Manual memory management MRC
First introduceReference Counter: Used to save the current object. There are several items in use (number)
Function of referencing a counter: Used to determine whether the object should reclaim the memory space (if the object is not equal to nil, when the reference counter is 0, the object's memory space should be reclaimed)
Reference Counter operations:
If an object is released, the dealloc method of the object is called.
Note:
- The dealloc method is NSObject. Generally, we need to override the dealloc method.
- Inside the dealloc method, call [super dealloc];
Memory Management scope:
Memory Management Principles:
Memory Management Research content:
Set Method Memory Management
// Dog * _ dog; // For the instance variable of the object as another class-(void) setDog :( Dog *) dog {// determine whether the object is the original object if (_ dog! = Dog) {// 2) the old value of release [_ dog release]; // The New Value of retain, and the value is assigned to the instance variable _ dog = [dog retain];}
Loop retain Problems
Loop retain will cause both objects to leak memory
Prevention Method:
Memory Management Issues of the NSString class
# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {@ autoreleasepool {// defines the constant pool of the string // string, // if the required string already exists in the constant pool, no memory space will be allocated. // when the string is used, // @ "abc" stringWithString alloc initWithString are all in the constant zone // 0x100001030 small NSString * str1 = @ "abc "; // @ "abc" String constant NSString * str3 = [NSString stringWithString: @ "abc"]; // constant NSString * str5 = [[NSString alloc] initWithString: @ "abc"]; // also in the constant area NSString * str6 = [[NSString alloc] init]; // constant area str6 = @ "abc "; // 0x100202030 large // If the str2 str4 address in the constant zone should be the same // actually different, so str2 str4 is in the heap zone NSString * str2 = [NSString stringWithFormat: @ "abc"]; // It is not in the stack zone. In the stack zone, NSString * str4 = [[NSString alloc] initWithFormat: @ "abc"]; // It is not in the stack zone, in the heap zone // 0x7fff5fbff764 int a = 10; // stack zone NSLog (@ "str1 =%@, % p, % lu", str1, str1, str1.retainCount ); NSLog (@ "str2 =%@, % p, % lu", str2, str2, str2.retainCount); NSLog (@ "str3 =%@, % p, % lu ", str3, str3, str3.retainCount); NSLog (@ "str4 =%@, % p, % lu", str4, str4, str4.retainCount); NSLog (@ "str5 =% @, % p, % lu ", str5, str5, str5.retainCount); NSLog (@" str6 = % @, % p, % lu ", str6, str6, str6.retainCount ); NSLog (@ "a = % p", & a) ;}return 0 ;}
Auto Release pool:Special stack structure
Features:
- Objects can be added to the Auto Release pool.
- When the automatic release pool ends, a release message is sent to the objects in the pool.
Automatic release pool usage:
@autoreleasepool { }
2. Add the Auto Release pool
/* [Object autorelease] in the Auto Release pool; */
Simulate an object method in the Person class-(void) run;
# Import <Foundation/Foundation. h> # import "Person. h "int main (int argc, const char * argv []) {// 1 create an automatic release pool Person * p = [Person new]; // p 1 @ autoreleasepool {// The automatic release pool starts [p run]; NSLog (@ "% lu", p. retainCount); // 1 // [p autorelease] Add the object p to the Auto Release pool. // Note: After the object p is added to the Auto Release pool, the reference count will not change [p autorelease]; // Add the Auto Release pool, NSLog (@ "% lu", p. retainCount); // 1 [p run];} // The automatic release pool ends [p release]; [p run]; return 0 ;}
We can add a class method to Person so that the created object will be added to the Auto Release pool.
+ (Instancetype) person {// Person person ---> Person // Stduent person ----> Student // create an object return [[[self alloc] init] autorelease]; // space of the returned object // It can help us add the object to the Auto Release pool}