Properties of memory management and @property
Directory
- Understanding of memory management
- Management of Memory
Ownership and memory management principles for objects
Rationally solve the problems caused by memory management
Auto Free Pool
- Properties of the @property
Understanding of memory management
Management of Memory
Ownership and memory management principles for objects
Both an object and a function can have administrative rights on the class object, so objects and functions need to be responsible for creating and releasing class objects
A reasonable solution to the problems caused by memory management (in the MRC, there is a release of the owning object on the Dealloc)
Stage One
MyClass *myclass = [[MyClass alloc] init];
MyClass's behavior
[MyClass release];
Phase II
-(void) Setengine: (engine *) newengine{engine = Newengine;}
Car *car = [[Car alloc] init];
Engine *engine = [[Engine alloc] init]; 1
[Car setengine:engine]; 1
[Engine release]; 0 causes the engine in the car object to point to an empty object
Stage Three
Adjustment to Setter:-(void) Setengine: (engine *) newengine{engine = [newengine retain];};
A problem exists:
Engine *engine1 = [engine new]; 1
[Car setengine:engine1]; 2
[Engine1 release]; 1
Engine *engine2 = [engine new]; 1
[Car setengine:engine2]; 1
[Car release]; 1 The Engine1 is not released at this time, that is, the object that the car originally pointed to was not released
Stage Three
Continue adjustment to setter:-(void) Setengine: (engine *) newengine{[engine release]; engine = [newengine retain]; }
There is a problem
Engine *engine = [engine new]; 1
Car *car1 = [car new];
Car *car2 = [car new];
[Car1 Setengine:engine]; 2
[Engine release]; 1
[Car2 SETENGINE:[CAR1 engine]]; Error at this point the engine's reference count is 0,CAR1 and CAR2 cannot use its Engine property
Stage Four
Continue adjustment to setter:-(void) Setengine: (Engine *) newengine{[newengine retain]; [Engine release]; engine = Newengine; }
Auto Free Pool
Properties of the @property
Properties for memory management and @property