//
Main.m
03-Memory management analysis for multiple objects
//
Created by Apple on 14-3-17.
//
//
#import <Foundation/Foundation.h>
People have a car
#import "Person.h"
1. Wild pointer problem
2. Memory leak issues
int main (int argc, const char * argv[])
{
@autoreleasepool {
1
person * p = [[Person alloc] init];
1
Car * car = [[Car alloc] init];
2
[P Setcar:car];
[P Drive];
1
[Car release];
To ensure that the object P exists, any method that invokes the object p does not go wrong
[P Drive];
0
[P release];
}
return 0;
}
//
Person.m
01-Manual memory management Basic concepts
//
Created by Apple on 14-3-17.
//
//
#import "Person.h"
@implementation person
-(void) Setcar: (Car *) car
{
//2, able to make Retaincount + 1 and return to self
_car = [car retain];
}
-(CAR *) car
{
return _car;
}
-(void) drive
{
[_car Run];
}
-(void) dealloc
{
The goal is to ensure that when the P object exists, the car object must exist
When the object P is destroyed,
[_car release];
[Super Dealloc];
NSLog (@ "person has been destroyed");
}
@end
Memory management analysis for multiple objects