NSImageView - 巨大的内存泄漏?

时间:2013-03-30 21:09:49

标签: objective-c macos cocoa memory-leaks nsimageview

由于某种原因,两个NSImageView的分配会造成巨大的内存泄漏 虽然在dealloc s。{/ p>中都会调用NSImageView方法

我正在使用ARC


NSImageView s

一旦调用了以下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方法的时候 第二种是窗口被解除分配。


没有NSImageView s

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

这里没有内存泄漏...


有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

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

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

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

您可以使用:

NSLog(@"Location = %@",NSObjectHere);

获取内存位置。如果匹配,则再次返回,并找到您的参考。除非它是一个ARC错误,这是可能的,虽然不太可能,但你在某个地方持有它的引用。

如需更多详细信息,请使用更多代码更新您的问题,没有人可以为您调试,而无需查看更多代码。

祝你好运!

修改

教程解释在http://mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/

使用泄漏工具