由于某些原因,两个NSImageView
的分配会造成巨大的内存泄漏。
尽管在两个dealloc
中都调用了NSImageView
方法。
我正在使用ARC
。
使用NSImageView
一旦调用以下getter方法,就会使用大量内存。
可以,但是在关闭窗口时不会取消分配...
- (NSView *)animationView {
if (!_animationView) {
_animationView = [[NSView alloc] initWithFrame:self.bounds];
self.oldCachedImageView = [[NSImageView alloc] initWithFrame:self.bounds];
[_animationView addSubview:self.oldCachedImageView];
self.oldCachedImageView.wantsLayer = YES;
self.cachedImageView = [[NSImageView alloc] initWithFrame:self.bounds];
[_animationView addSubview:self.cachedImageView];
self.cachedImageView.wantsLayer = YES;
self.animationView.wantsLayer = YES;
}
return _animationView;
}
第一个圆圈是调用上面的getter方法时。
第二个是重新分配窗口的时间。
没有
NSImageView
没有将两个
NSImageView
注释掉。- (NSView *)animationView {
if (!_animationView) {
_animationView = [[NSView alloc] initWithFrame:self.bounds];
/*
self.oldCachedImageView = [[NSImageView alloc] initWithFrame:self.bounds];
[_animationView addSubview:self.oldCachedImageView];
self.oldCachedImageView.wantsLayer = YES;
self.cachedImageView = [[NSImageView alloc] initWithFrame:self.bounds];
[_animationView addSubview:self.cachedImageView];
self.cachedImageView.wantsLayer = YES;
self.animationView.wantsLayer = YES;
*/
}
return _animationView;
}
这里没有内存泄漏...
有人知道为什么会这样吗?
最佳答案
使用仪器追踪“泄漏”。仪器中有一个泄漏工具。一直使用您的应用程序运行它,直到完全关闭它。确保ARC不在以后重新分配它,这是可能的。将其保留在内存中一段时间,以便在需要时可以更快地进行检索。
但是,如果确实看到泄漏,则说明存在问题。确保没有注意到您没有注意到的引用。
如果显示泄漏,请使用泄漏工具找出泄漏的内存位置。然后尝试将其与代码中的某些对象匹配。换句话说,请确保泄漏的是NSImageView。
您可以使用:NSLog(@"Location = %@",NSObjectHere);
获取内存位置。如果匹配,请再返回一次,找到您需要的参考。除非这是一个ARC错误,这是有可能的,尽管可能性不大,否则您将在某个地方保留对它的引用。
有关更多详细信息,请使用更多代码更新您的问题,没有人可以看到更多代码为您调试它。
祝好运!
编辑
在http://mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/处解释如何使用泄漏工具的教程