Nic
Nic

Reputation: 4977

Huge memory leak in NSBitmapImageRep

I have a method that analyzes pixel data inside an NSBitmapImageRep that is built from a CGImageRef. Here is the relevant code:

CGImageRef ref;
// omitted code for initializing ref
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:ref];
uint32* bitmapPixels = (uint32*) [bitmapRep bitmapData];
// do stuff with bitmapPixels
[bitmapRep release];
CGImageRelease(ref);

I know I'm properly releasing the CGImageRef and NSBitmapImageRep, but the call to -bitmapData leaks about 2 MB each time it's called, and I don't know how to properly release it. Any ideas?

Update: I forgot to add one important point: memory is only leaked when there is a full screen application running. For regular usage, the memory is released just fine.

Upvotes: 1

Views: 1766

Answers (2)

Ken
Ken

Reputation: 13003

Are you doing this in a loop? If so you might need to make an autorelease pool to make sure memory is cleaned up in a timely fashion. See autorelease pools.

Upvotes: 4

Peter Hosey
Peter Hosey

Reputation: 96323

The bitmap data should be owned by either the CGImage or the NSBitmapImageRep (or copied to an autoreleased behind-the-scenes NSData object by the latter). As such, whichever object owns (or copies) it should release it.

Are you seeing contrary results in heap/Instruments?

Upvotes: 0

Related Questions