0

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?

IluTov
  • 6,807
  • 6
  • 41
  • 103
  • Well, where is your dealloc method? Or is this on ARC? – Josiah Mar 30 '13 at 21:14
  • Yes this is `ARC`, sorry, forgot to write it – IluTov Mar 30 '13 at 21:14
  • Okay, ARC works by auto-deallocating things when it detects there are no pointers, well, pointing to it. Make sure you are not keeping a reference to it somewhere else. Also, remember you are not just looking at the NSImageView. Really you should check to make sure you are deallocating the NSImage. Where is your NSImage getting added? Perhaps some code there would help. – Josiah Mar 30 '13 at 21:17
  • Im not. This is the only reference – IluTov Mar 30 '13 at 21:18
  • You have an NSImageView with no NSImage in it? – Josiah Mar 30 '13 at 21:20
  • Yes, for testing its empty – IluTov Mar 30 '13 at 21:21
  • Okay, then I would double, triple, check that you aren't holding a reference to these NSImageViews. – Josiah Mar 30 '13 at 21:23
  • Why don't you use the [leaks instrument](http://developer.apple.com/library/ios/#Documentation/AnalysisTools/Reference/Instruments_User_Reference/LeaksInstrument/LeaksInstrument.html) alongside the allocations instrument? This will tell you what objects are leaking and the lines of code in your project responsible for the leaks. Additional information [here](https://developer.apple.com/library/ios/#documentation/Performance/Conceptual/ManagingMemory/Articles/FindingLeaks.html) – Elliott Mar 30 '13 at 21:24
  • Also, if your run the instruments for Leaks, do you get any. It's possible ARC is holding it for a while in memory in case it is used again. That would make a faster retrieval. You are only showing a small portion of the trace, are you sure it's not getting deallocated at a later time? – Josiah Mar 30 '13 at 21:25
  • @ElliottPerry It actually doesn't show any leaks... But there is no reference anywhere anymore... I don't know what else it should be if not a leak. – IluTov Mar 30 '13 at 21:50
  • I believe @Josiah could have accurately described your situation. We don't know what is happening under the covers, its possible that UIKit caches some content and holds onto references longer than you expect. In any case, as long as there are no leaks and the memory usage doesn't lead to spikes that could result in your app being terminated, there's nothing to worry about. – Elliott Mar 30 '13 at 21:54
  • @ElliottPerry May be. After 2 min the memory is still not released, no leak. But it's not much memory, so I think it's not that big of a problem. – IluTov Mar 30 '13 at 21:59

1 Answers1

1

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/

Josiah
  • 4,663
  • 2
  • 31
  • 49
  • Thanks for your answer. However, the "Leaks" trace tool in Instruments does not show any leaks. But there are really no references, no windows, no views left. On every object, dealloc is called. I have no idea why there is so much memory not deallocated. And yes, I waited some time to see if the memory would be released later, but it didn't. the printscreens here are smaller just so it can fit on the screen. – IluTov Mar 30 '13 at 21:53
  • If the leaks tool shows nothing, then there is no leak. Even if you don't see the memory going down, Instruments may be able to tell that it is caching the NSImageView and therefore will deallocate it later. Because of this, it should not shoe any leaks, and everything is fine. – Josiah Mar 30 '13 at 22:22