在MRC下
@property (nonmatic,strong)NSArray *currentArray;
- (void)viewDidLoad
{
? ? [super viewDidLoad];
? ? self.currentArray = [NSArray new];
}
- (void)dealloc
{
? ? [_currentArray release];
? ?[super dealloc];
}
這樣會導致內存泄露,因為創建array 的時候,retaincount是1,賦值給屬性(setter)方法的時候,retaincount 值增加1。而在dealloc的時候,只是釋放了一次,所以會導致內存泄露。修改如下,self.currentArray = [NSArray array]; 這樣的屬性retain的dealloc釋放,類方法產生的是autorelease對象。如下(strong 的setter方法)
- (void)setCurrentArray:(NSArray *)currentArray
{
? ? if(_currentArray != currentArray)
? ? {
? ? ? ? [_currentArray ?release];
? ? ? ? _currentArray = [currentArray retain];
? ? }
}