SAPostTableViewController *postViewController = [[SAPostTableViewController alloc] initWithNibName:NSStringFromClass([SAPostTableViewController class]) bundle:nil];
postViewController.site = site;
[self.navigationController pushViewController:postViewController animated:YES];
[postViewController release];
SAPostTableViewController具有静态tableView,其中的单元格是从nib中加载的。我已经重写了
initWithNibName:bundle:
方法:-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.sections = [NSMutableDictionary dictionary];
}
return self;
}
sections
是一个保留的属性。
在 SAPostTableViewController
的 viewDidLoad
中我有这段代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cellVisibiltyChanged:) name:@"SAStaticCellVisibiltyChanged" object:nil];
}
因此,为了匹配viewDidUnload
:
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"SAStaticCellVisibiltyChanged" object:nil];
}
然而,当我按导航栏中的“返回”按钮(所有标准行为,没有覆盖)并弹出
SAPostTableViewController
时,它不会调用viewDidUnload
或dealloc
。这意味着如果我重新选择推送SAPostTableViewController
的单元格,它将创建SAPostTableViewController
的新实例,反复进行此操作只会导致内存使用量增加,因为弹出的SAPostTableViewController
从未被释放。(我通过在分配上运行Instruments来了解这一点)奇怪的是,如果我两次释放
SAPostTableViewController
,那么它就像我期望的那样工作:SAPostTableViewController *postViewController = [[SAPostTableViewController alloc] initWithNibName:NSStringFromClass([SAPostTableViewController class]) bundle:nil];
postViewController.site = site;
[self.navigationController pushViewController:postViewController animated:YES];
[postViewController release];
[postViewController release];
如果我添加第三个释放语句,它会像我预期的那样崩溃,就像只有2个时一样。
我已经采用了使用retainCount
并逐步执行直接上面代码行中执行的行的方法,在第一行代码执行完毕时,retainCount保持为1。它在第一行和第二行之间跳跃,所以我找不到任何地方会多保留它一次?
SAPostTableViewController仅在此处使用,它既不是任何东西的委托,也没有委托。
我该如何找到修复方法,或者是我忽略了简单的东西吗?
这是Instruments在推送SAPostTableViewController后仅一次显示的内容(仅有一个释放语句):
![enter image description here](https://istack.dev59.com/ym2eo.webp)
这是在反复导航回去和前进后Instruments显示的内容(同样,只有一个释放语句):
![enter image description here](https://istack.dev59.com/MgwXc.webp)
SAPostTableViewController
? - Dave DeLong[NSMutableDictionary dictionary]
- 你是不是想用[[NSMutableDictionary alloc] init]
? - ott--