标签:objective-c ios 内存管理 循环引用
1 @class// Person.h @class Car @interface Person @property(noatomic, retain) Car* car; @end
// Person.m #import "Person.h" @implementation Person - (void)dealloc { [_car release]; _car = nil; [super dealloc]; } @end
// Car.h @class Person @interface Car @property(noatomic, retain) Person* person; @end
// Car.m #import "Car.h" @implementation Car - (void)dealloc { [_person release]; [super dealloc]; } @end
// main.m #import "Car.h" #import "Person.h" int main() { Person* person = [[Person alloc] init];// person 1 Car* car = [[Car alloc] init];// car 1 person.car = car;// car 2 car.person = person;// person 2 [car release];// car 1 [person release];// person 1 return 0; }
// Car.h @class Person @interface Car @property(noatomic, assign) Person* person; @end
// Car.m #import "Car.h" @implementation Car - (void)dealloc { [super dealloc]; } @end
// main.m #import "Car.h" #import "Person.h" int main() { Person* person = [[Person alloc] init];// person 1 Car* car = [[Car alloc] init];// car 1 person.car = car;// car 2 car.person = person;// person 1 [car release];// car 1 [person release];// person 0,car 0 return 0; }
标签:objective-c ios 内存管理 循环引用
原文地址:http://blog.csdn.net/xufeng0991/article/details/43273045