Design Element
Comment on "Proportional Scaling with NSImage"
by BrentC — Nov 07
Thanks for a very useful column for a newbie to the Cocoa/ObjC world...

I think I may have found a problem with your -imageByScalingProportionallyToSize implementation, though, at least as it is used in the SimplePicture sample that I was playing with.

The issue shows itself when you browse a directory with very large pics... in my case, a directory with 114 8 MPixel images from my camera. What I saw initially was the SimplePic process immediately max out on virtual memory, and grind the system to a halt while processing the directory.

My initial investigations didn't find any memory leaks... what I did find (using the ObjAlloc tool), though, was a huge amount of memory being allocated in the call to NSImage:isValid.

On a hunch (being new to this ObjC stuff...) I thought that there might be a bunch of memory being autoreleased in that hidden OS code... and since 114 huge pictures are loaded in between auto-release pool releases, it seems that the auto-released (but not actually released) image buffers clog the system.

So, in NSImage-Extras.m, I added:
- (NSImage*)imageByScalingProportionallyToSize:(NSSize)targetSize { NSImage* sourceImage = self; NSImage* newImage = nil; [b]NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];[/b]

and then:
[b]// remember to release our temporary auto-pool [pool release]; // And then add the image to the auto-release pool of the caller...[/b] return [newImage autorelease]; } @end

Is this the 'correct' way to handle this in ObjC? It seems to solve the problem, as memory usage is now correct, and any size directory now parses (in time...). But is there a better way?

Thanks again for a very useful set of columns.
Back to "Proportional Scaling with NSImage"
Design Element

Copyright © Scott Stevenson 2004-2015