I have an OSX Cocoa ARC project that handles thousands of images. In order to reduce the number of images, we scan the images to see if they are a uniform color, and if so, we discard the image and keep track of its color.
The code to get a solid color is:
- (NSColor *)getSolidColor:(NSImage *)image
{
NSBitmapImageRep* raw_img = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
int Bpr = [raw_img bytesPerRow],spp = [raw_img samplesPerPixel];
unsigned char *data = [raw_img bitmapData];
int w = [raw_img pixelsWide],h = [raw_img pixelsHigh];
uint32_t mask = 0x00FFFFFF;
uint32_t color = *((uint32_t *)data) & mask;
for( int y=0; y<h; y++ )
{
unsigned char *p = data + Bpr*y;
for( int x=0; x<w; x++ )
{
if( color != (*((uint32_t *)p) & mask) )
return( nil );
p += spp;
}
}
return( [raw_img colorAtX:0 y:0] );
}
(Some error checking removed for brevity - above code assumes 3 samples per pixel.)
The code that calls it is basically:
NSString *imageFile;
while( imageFile = [self getNextImageFile] )
{
NSImage *image = [[NSImage alloc] initWithContentsOfFile:imageFile];
if( [image isValid] && [self getSolidColor:image] )
[fileManager removeItemAtPath:imageFile error:nil];
}
The problem is that the application consumes an exraordinary amount of memory. When I run it in the profiler, it indicates that 90% of the memory used is being allocated by [NSImage TIFFRepresentation] (bolded above.)
i.e. The TIFFRepresentation data and NSBitmapImageRep are never freed even though they fall out of scope as soon as the function returns.
Why? And what should/can I do to force ARC to release those blocks?
With the 'old way' using non-ARC I would just put an autorelease pool inside the while loop for the images and that would take care of the problem. Is there such a concept with ARC?
Thanks.
(PS. NSZombies is NOT enabled.)