gpt4 book ai didi

objective-c - NSImageView - 巨大的内存泄漏?

转载 作者:行者123 更新时间:2023-12-03 17:54:33 25 4
gpt4 key购买 nike

出于某种原因,分配两个 NSImageView 会造成巨大的内存泄漏。
尽管 dealloc 方法在两个 NSImageView 中都会被调用。

我正在使用ARC

<小时/>

使用NSImageViews

一旦调用以下 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;
}

enter image description here

第一个循环是调用上面的 getter 方法时。
第二个是窗口被释放时。

<小时/>

没有NSImageViews

没有注释掉两个 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;
}

enter image description here

这里没有内存泄漏...

<小时/>

有人知道为什么会发生这种情况吗?

最佳答案

使用仪器来追踪“泄漏”。 Instruments 中有一个 Leaks 工具。通过使用您的应用程序一直运行它,直到您完全关闭它。确保 ARC 不仅仅在稍后释放它,这是可能的。将其保留在内存中一段时间​​可以在再次需要时更快地检索。

但是,如果您确实看到显示泄漏,则存在问题。确保没有任何您未注意到的引用。

如果显示泄漏,请使用泄漏工具找出正在泄漏的内存位置。然后尝试将其与代码中的某个对象进行匹配。换句话说,确保泄漏的是 NSImageView。

您可以使用:

NSLog(@"位置 = %@",NSObjectHere);

获取内存位置。如果匹配,则再次返回并找到您所拥有的引用资料。除非它是一个 ARC bug(虽然可能性不大,但有可能),否则您会在某处保存对它的引用。

有关更多详细信息,请使用更多代码更新您的问题,没有人可以在没有看到更多代码的情况下为您调试它。

祝你好运!

编辑

解释 Leaks 工具使用的教程位于 http://mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/

关于objective-c - NSImageView - 巨大的内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15723516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com