Question

For some reason, the allocation of two NSImageViews creates a huge memory leak.
Although dealloc method gets called in both NSImageViews.

I'm using ARC.


With NSImageViews

As soon as the following getter method is called, a lot of memory is used.
This would be ok, but it doesn't get deallocated when the window is being closed...

- (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

The first circle is when the getter method above is called.
The second is when the window is deallocated.


Without NSImageViews

Not with the two NSImageViews commented out.

- (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

No memory leak here...


Anyone an idea why this happens?

Was it helpful?

Solution

Use Instruments to trace the "Leaks". There is a Leaks tool in Instruments. Run it through the use of your app, all the way until you close your it completely. Make sure ARC isn't just deallocating it at a later time, which is possible. Keeping it in memory for a while allows for a faster retrieval if needed again.

However, if you do see a leak displayed, then there is an issue. Make sure there are no references to it you aren't noticing.

Use the leaks tool to figure out the memory location that is leaking if it displays some. Then try to match it with some object in your code. In other words, make sure it is the NSImageView that is leaking.

You can use:

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

To get the memory location. If it matches, then go back, once more, and find the reference you have to it. Unless it's an ARC bug, which is possible, though unlikely, then you are holding a reference to it somewhere.

For any more detail, please update your question with more code, no one can debug it for you without seeing more code.

Good luck!

EDIT

Tutorial explaining the use of the Leaks tool at http://mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top