【发布时间】:2010-07-19 16:49:17
【问题描述】:
所以 Instruments 告诉我,我有三个源自此方法的内存泄漏(具体来说,它指出了这一行: [self.view addSubview:menuBar.view];
我看不到泄漏,我绞尽脑汁。我保留了对 menuBar 对象的引用并正在释放它。比我聪明的人能解释一下吗?我的 XIB 中有三个菜单栏项并且出现三个泄漏是巧合吗?
这是整个方法:
// root vc 调用来切换屏幕上菜单栏的显示状态
-(IBAction) showToolBar {
//if no toolbar exists, create one and add it to the view
if (!menuBarView) {
MenuBarViewController *menuBar = [[MenuBarViewController alloc] initWithNibName:@"MenuBarViewController" bundle:nil];
menuBar.book = self.selectedTitleDeck;
menuBar.booksArray = self.allTitleDeck;
self.menuBarView = menuBar;
[self.view addSubview:menuBar.view];
[menuBar release];
}
CGRect frame = menuBarView.view.frame;
[UIView beginAnimations:nil context:NULL];
if (self.toolBarIsDisplayed == NO) {
//show the toolbar
frame.origin.y = 725;
self.toolBarIsDisplayed = YES;
} else if (self.toolBarIsDisplayed == YES) {
//hide the toolbar
frame.origin.y = 788;
self.toolBarIsDisplayed = NO;
}
self.menuBarView.view.frame = frame;
[UIView commitAnimations];
}
【问题讨论】:
-
只是检查一下您是在设备上看到的,而不是在模拟器中看到的,对吧?
-
同意尼克的问题——这是在设备上还是模拟器上?检查泄漏时禁止使用模拟器。顺便说一句,我最后一次在 addSubview: 中看到泄漏是在我的一个多线程应用程序中,我在主线程之外进行了 UI 调用。你的应用不是多线程的吗?
-
我几天来一直试图找到这个漏洞。是的,我是在模拟器中做的。切换到设备,没有发现泄漏。感觉很愚蠢,但是,我想,生活和学习。谢谢大家!
标签: objective-c iphone memory-management