我对一个对象进行了五次分配,而同一函数中只有一次分配
我正在编写一个仅在弹出窗口中显示菜单(由 tableView 组成)的函数。
这是源代码:
-(void)pushSearch:(NSString *)option type:(int)optionType
{
searchNav = [[iNavigation alloc] initWithNibName:@"iNavigation" bundle:nil] ;
//This is the UIViewController with the tableView
[searchNav setSearchMode:optionType];
searchNav.view.frame =CGRectMake(0,0,300,600);
NSLog(@"Retain Count: %d",[searchNav retainCount]);
//At this point keep count is 1
if ([pop isPopoverVisible])
{
[pop dismissPopoverAnimated:YES];
[pop release];
}
pop = [[UIPopoverController alloc] initWithContentViewController:searchNav];
NSLog(@"Retain Count: %d",[searchNav retainCount]);
//At this point retain count is 2
[pop presentPopoverFromRect:btnMenu.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[pop setPopoverContentSize:CGSizeMake(350,600)];
NSLog(@"Retain Count: %d",[searchNav retainCount]);
//At this point retain count is 5!!!
[searchNav release];
}
我遇到的问题是加载表视图所使用的内存永远不会被释放。我的应用程序在内存中不断增长,直到崩溃。
为什么如果我只为 searchNav 分配一次,在将其分配给 popOver 后,reatin 计数为 5?
请问有什么帮助吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用-retainCount。
对象的绝对保留计数是没有意义的。
您应该调用
release
的次数与导致对象被保留的次数完全相同。不能少(除非你喜欢泄漏),当然也不能多(除非你喜欢崩溃)。请参阅内存管理指南了解完整详细信息。
保留计数为 5 是无关紧要的,而且很可能是因为各个框架的内部实现细节。
该代码中
searchNav
的内存管理是正确的;您分配了该对象(+1 保留),然后最终释放了它(-1 保留)。因此,内存泄漏是在其他地方。尝试在您的源上使用“构建和分析”。然后使用分配工具来查看随着应用程序随着时间的推移而增长而悬挂的对象。某处存在过度保留;您尚未将其与释放进行平衡的保留。
Do not use -retainCount.
The absolute retain count of an object is meaningless.
You should call
release
exactly same number of times that you caused the object to be retained. No less (unless you like leaks) and, certainly, no more (unless you like crashes).See the Memory Management Guidelines for full details.
That the retain count is 5 is irrelevant and quite likely because of internal implementation details of the various frameworks.
The memory management of
searchNav
in that code is correct; you allocated the object (+1 retain) and then you eventually released it (-1 retain). Therefore, the memory leak is somewhere else.Try using "build and analyze" on your source. Then use the Allocations instrument to have a look at the objects that are hanging about as your app grows over time. There is an over-retain somewhere; a retain for which you haven't balanced it with a release.
你释放了
UIPopoverController
吗?也许该对象仍然保留表视图。Do you release the
UIPopoverController
? Perhaps that object is still retaining the table view.