views:

268

answers:

2

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.

A: 

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?

Peter Hosey
+1  A: 

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.

Ken