#import <Foundation/Foundation.h> @class Card; @interface Person : NSObject //@property(nonatomic,retain)NSString *name; @property (nonatomic,assign)Card * card; @end
#import "Person.h" #import "Card.h" @implementation Person - (void)dealloc
{ NSLog(@"person released"); [super dealloc]; } @end
#import <Foundation/Foundation.h> #import "Person.h" @interface Card : NSObject @property(nonatomic,retain) Person * p; @end
#import "Card.h" @implementation Card - (void)dealloc { NSLog( @"card released......."); [_p release]; [super dealloc]; } @end
////////////解决循环引用的方法//////////////////////
//循环引用的的两个对象,一端使用assign,另外一端使用retain,这样在p1.card = c1;这句话过后
//c1的计数器依然是1(因为是使用的assign),而在c1.p = p1;这句话过后,p1的计数器变成了2(因为是使用的retain).当[p1 release];执行过后,p1的计数器减到1,[c1 release];过后c1的计数器减到0,然后调用Card里面中的dealloc方法,这样,p1就会再release一次。最后两个计数器都会减到0