我正在尝试消除我的 iPhone 3.0 应用程序中的所有内存泄漏。CLANG 构建时所有灯都是绿色的,所以现在我正在使用 Instruments 进行分析。
这说起来容易做起来难,因为它表明在使用该应用程序几分钟后就有数百个 16 字节泄漏。它似乎主要在 UIKit 中,共同的部分是堆栈跟踪的末尾总是调用[NSObject respondsToSelector]
这是我可以忽略的事情还是所有这些泄漏的原因是什么?II 可以忽略它们,有没有办法在 Instruments 中将它们过滤掉,这样我就可以检测到真正的泄漏?
*编辑我设法找到导致问题的代码部分,但我仍然不明白为什么。我有一个带有一些文本的自定义 UIView 和一个在异步 http 请求期间可见的微调器。请求完成后,我在视图上调用此方法:
- (void)fadeOut{
spinner.hidden = YES;
loadingLabel.hidden = YES;
messageLabel.hidden = YES;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(fadeComplete)];
[UIView setAnimationDuration:0.40];
self.alpha = 0.0;
[UIView commitAnimations];
}
- (void)fadeComplete{
[self removeFromSuperview];
}
如果我只是简单地做
[self removeFromSuperView]
如果没有 alpha 动画,则不会报告泄漏。
有关仪器的详细信息,请参见下面的屏幕截图。
示例堆栈跟踪:
0 libobjc.A.dylib _malloc_internal
1 libobjc.A.dylib _cache_addForwardEntry
2 libobjc.A.dylib lookUpMethod
3 libobjc.A.dylib class_respondsToSelector
4 CoreFoundation -[NSObject respondsToSelector:]
5 UIKit -[UINavigationTransitionView transition:fromView:toView:]
6 UIKit -[UINavigationTransitionView transition:toView:]
7 UIKit -[UINavigationController _startTransition:fromViewController:toViewController:]
8 UIKit -[UINavigationController _startDeferredTransitionIfNeeded]
9 UIKit -[UINavigationController viewWillLayoutSubviews]
10 UIKit -[UILayoutContainerView layoutSubviews]
11 UIKit -[UIView(CALayerDelegate) _layoutSublayersOfLayer:]
12 QuartzCore -[CALayer layoutSublayers]
13 QuartzCore CALayerLayoutIfNeeded
14 QuartzCore CA::Context::commit_transaction(CA::Transaction*)
15 QuartzCore CA::Transaction::commit()
16 QuartzCore CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*)
17 CoreFoundation __CFRunLoopDoObservers
18 CoreFoundation CFRunLoopRunSpecific
19 CoreFoundation CFRunLoopRunInMode
20 GraphicsServices GSEventRunModal
21 GraphicsServices GSEventRun
22 UIKit -[UIApplication _run]
23 UIKit UIApplicationMain
24 Client main **/main.m:14
25 Client start
还有一个:
0 libobjc.A.dylib _malloc_internal
1 libobjc.A.dylib _cache_addForwardEntry
2 libobjc.A.dylib lookUpMethod
3 libobjc.A.dylib class_respondsToSelector
4 CoreFoundation -[NSObject respondsToSelector:]
5 UIKit -[UIViewAnimationState animationDidStart:]
6 QuartzCore run_animation_callbacks(double, void*)
7 QuartzCore CA::timer_callback(__CFRunLoopTimer*, void*)
8 CoreFoundation CFRunLoopRunSpecific
9 CoreFoundation CFRunLoopRunInMode
10 GraphicsServices GSEventRunModal
11 GraphicsServices GSEventRun
12 UIKit -[UIApplication _run]
13 UIKit UIApplicationMain
14 Client main ***/main.m:14
15 Client start