Memory management
Overriding the Dealloc method
@implementationDog#pragmaMark when an object is removed from memory, it is called-(void) dealloc{NSLog (@"----Dog was destroyed------"); //Be sure to call the Super method, written at the end of the method[Super Dealloc];} @endintMain () {Dog*dog=[[Dog alloc] init]; [Dog release];//reclaims object memory, after which the dog becomes a wild pointer (pointing to unavailable memory)Dog=Nil; [Dog release]; //Send a message to the null pointer, nothing happens. return 0; }
The code can get the value of the object counter by calling the Retaincount method with a pointer.
Memory management of member variables (if member variables are objects, management is required)
1, the management of the Set method: *release the member variable, retain the new member variable (need to determine whether the old and new member variables are the same)
2. Management of Delloc methods: *release currently owned member variables
Second, @property parameters
After @property with the parameter retain, the compiler can automatically generate declarations and definitions for the set and get methods that contain memory management. Format: Example: @property (retain) Cat *cat;
Parameter function:
1. Memory management of Control set method
1>retain:release the old value, Ratain the new value, the object member must be of this type.
2>assign (default): Direct assignment without any memory management
3>copy:
2, control there is no set method and get method
1>readwrite (default): Both the Set method and the Get method are generated. Example: @property (retain,readwrite) Cat *cat;
2>readonly: Only the Get method is generated (that is, it can only be read and cannot be assigned)
3. Multithreading Management
1>atomic (default): Low performance.
2>nonatomic: High Performance
4. Control the name of the set method and the Get method (that is, do not use the default method name)
1>setter: Set the name of the set method, there must be a colon
2>getter: Set the name of the Get method
@property (nonatomic,assign,getter=getheight,setter=setmyheight:) int height;//The Get method is named GetHeight, The Set method is named Setmyheight:
Third, Autorelease
How to use: Person *p=[[[person alloc] init] autorelease];
Autorelease returns the object itself, acting: Autorelease will add the object to the pool, but the counter does not change. Note: When the pool is destroyed (that is, the end of the @autoreleasepool), all objects in the pool will do a release operation (note that it is not necessarily released, only the counter minus one), the iOS pool is created and destroyed by the system (there may be multiple pools in the system), or you can create the pool yourself , so the specific pool creation and destruction time is uncertain, it should be used sparingly autorelease.
Apple's official approach (API):
1> If the method name is not alloc, new, there is no release or autorelease,
2> If the method name is Alloc, new, it must be either release or Autorelease