-2

在这里找到一个奇怪的代码我有一个视图控制器,它有一个带有书籍的数组,然后单击单元格,然后推送到一个 detailViewController,detailVC 有一个变量 infoDict,

 @property (nonatomic,retain) NSMutableDictionary * infoDict;

导航控制器推送

DetailViewController * tDVC = [[DetailViewController alloc] init];
tDVC.infoDict = infoDict;
[self.navigationController pushViewController:tDVC animated:YES];
[tDVC release];

点击返回按钮,弹回,在DetailVC的dealloc里面

-(void)dealloc
{
    [super dealloc];

    NSLog(@"before %d",infoDict.retainCount);
    [infoDict release];
}

但是当我点击返回时,在这个 dealloc 应用程序中随机崩溃,EXC_BAD_ACCESS。

当将 [super dealloc] 移动到 dealloc 的底部时,它似乎恢复了正常。请帮助我理解这一点,非常感谢

4

3 回答 3

4

[super dealloc] deallocates the object itself. If you type dealloc and let Xcode autocomplete, you get this:

- (void)dealloc
{
    <#deallocations#>
    [super dealloc];
}

Meaning you should release objects and properties before you call [super dealloc]

于 2013-08-29T07:05:39.487 回答
2

Your -dealloc implementation is out of order. The call to -[super dealloc] must be the absolute last invocation in the dealloc method. When you access the ivar infoDict, the compiler is really doing something like self->infoDict and by this point, self has been deallocated and is no longer valid.

If at all possible, I recommend using ARC instead of manually managing memory.

于 2013-08-29T07:06:29.853 回答
-1

尝试

如果你使用self.infoDict = nil;而不是更好[infoDict rlease];

-(void)dealloc
{
  NSLog(@"before %d",infoDict.retainCount);
  [infoDict release];

  [super dealloc];
}
于 2013-08-29T07:14:23.363 回答