I am battling a leak in my code and can't work out what I am doing wrong. I 
have a loop which accesses the raw bitmap data from a series of TIFF files I 
load from disk. My calls to [bitmapRep bitmapData] seem to (understandably) 
allocate memory, but I cannot work out how to allow that memory to be released 
again. What am I missing?

I've found various threads including this one:
        
http://stackoverflow.com/questions/4170799/objc-leak-behavior-i-cant-explain
that suggest autorelease pools should solve the problem. However the following 
code still leaks memory:




// Utility function used in main code:

NSBitmapImageRep *RawBitmapFromImage(NSImage *image)
{
    NSBitmapImageRep    *result = nil;
    NSArray             *repArray = [image representations];
        
    for (size_t imgRepresentationIndex = 0; imgRepresentationIndex < 
repArray.count; ++imgRepresentationIndex)
    {
        NSObject *imageRepresentation = [repArray 
objectAtIndex:imgRepresentationIndex];
        
        if ([imageRepresentation isKindOfClass:[NSBitmapImageRep class]])
                {
                        ALWAYS_ASSERT(result == nil);   // If we fail this then 
there are two different bitmap representations stored
                                                                                
        // (need to decide what to do then...)
                        result = (NSBitmapImageRep*)imageRepresentation;
                }
        }
        ALWAYS_ASSERT(result != nil);           // If we fail this then there 
was no bitmap representation stored (vector image?)
        return [[result retain] autorelease];
}

// Main code:

for (int i = firstIndex; i <= lastIndex; i++)
{
        printf("add frame from file \"%s\"\n", 
NSStringForFrameNumber(i).cString);
        NSImage *frameImage = [[NSImage alloc] 
initWithContentsOfFile:NSStringForFrameNumber(i)];

        // Temp code to debug serious memory leak
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

        NSBitmapImageRep *bitmapRep = RawBitmapFromImage(frameImage);
        /*      If I comment out the following line then there are no major 
leaks.
                With this line in place, bitmapData is leaked, in that 
ObjectAlloc
                shows a whole load of allocations from within the call to 
bitmapData
                that build up until all memory is full. */
        unsigned char * bitMapDataPtr = [bitmapRep bitmapData];

        [pool drain];
        [frameImage release];           // I have tried swapping these two 
calls around, to no effect (unsurprisingly)...
}




Please can somebody suggest what I am missing here? I am tearing my hair out 
over this one. I can post a full compilable demo if necessary.

Many thanks for any pointers on this one
Jonny_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to