NSView cacheDisplayInRect memory issue

Application that I am working process images. It’s like user drops at max 4 images and app layout them based on user’s selected template. One image might be added 2-3 times in final view.

Each image in layout is drawn in NSView (drawRect method of NSView using drawInRect method).Now final image (combined image by layouting all images) is created by saving NSView as Image and it all works very well.

Now problem that I am facing is memory is being retained by app once all processing is done. I have used instruments allocation and I don’t see memory leaks but I see “Persistent bytes” are increasing continuously with each session of app and one user reported issue in GB’s. Please see screenshot.


ImageIO_TIFF screen shot is at: http://i.imgur.com/UUIlvOP.png (can’t upload more than one image)
When I further investigated in Instruments I saw below code snaps of app that is causing memory retentions. All are related to ImageIO and coreImages. See below from instruments:

Instruments code snap : http://i.imgur.com/qquhpFp.png

However this seems to be only problem with 10.10 and above system. Tested same version of the app in 10.9.x and memory usage remains with in 60MB. During session execution in app it goes to 200MB but once it’s done it comes back to 50-60MB that usual for kind of app.

[_photoImage drawInRect: self.bounds fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0 respectFlipped: YES hints: nil];
            _photoImage = nil;

Above code I am using to draw image in NSView’s drawRect method and code shown in image is being used to get NSView as Image.

After my further investigation I found that it’s CGImageSourceCreateWithData that is caching the TIFF data of NSImage.
After replacing below line

CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imgData, NULL);

with

 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], NULL);

everything just started working at least on my 10.10.5 system but while running app on 10.11.x and other user’s 10.10.5 system, i am facing same issues. Application is quite big so I am not able to share it’s code.

If i don’t save NSView as image it does not at all cache any image so I am suspecting it is NSView cacheDisplayInRect method’s invocation that’s somehow retaining memory.

Please help, I have been struggling with this issue for about a week :frowning:

Thanks,
Mahesh.

Can anyone please help ?