Re: Image loading bug out of nowhere...
Re: Image loading bug out of nowhere...
- Subject: Re: Image loading bug out of nowhere...
- From: glenn andreas <email@hidden>
- Date: Thu, 3 Nov 2005 08:59:36 -0600
On Nov 3, 2005, at 6:15 AM, Michael Becker wrote:
Hi,
I have a very heavy bug in my application and don't even know where
to start looking for it. It has to do with loading JPEG images. I
read out the iPhoto library and load the thumbnail images. For
testing purposes, I created about 300 copies of one 1,2 MB JPEG
image and imported them into iPhoto.
Now when my program loads all those images, there's always a
certain point where no images will be loaded anymore and nothing
else works (e.g. clicking an "Open" Button in my application
results in an error message "unable to load nib data: /System/
Library/Frameworks/AppKit.framework/Resources/English.lproj/
NSNavPanelContentView.nib").
This is a classic symptom of running out of file descriptors - i.e.,
opening files and not closing them.
"newImage" in the code below will be an object of my own image
storage class.
Here's basically how I load the images (I want to know the original
image's size, however):
NSData *imageData = [[NSData alloc]
initWithContentsOfFile:thumbnailPath];
NSBitmapImageRep *bitmapImageRep = nil;
char buffer[3];
[imageData getBytes:&buffer length:3];
if (([[[path pathExtension] lowercaseString]
hasPrefix:@"jp"]) &&
(buffer[0] == -1) &&
(buffer[1] == -40) &&
(buffer[2] == -1))
{
bitmapImageRep = [[NSBitmapImageRep
imageRepWithContentsOfFile:thumbnailPath] retain];
// Load the original fullsize image to find out its
dimensions
NSData *fullsizeData = [[NSData alloc]
initWithContentsOfFile:path];
NSBitmapImageRep *sizeRep = [[NSBitmapImageRep
alloc] initWithData:fullsizeData];
// Set original image size in our new object
[newImage setImageSize:NSMakeSize([sizeRep
pixelsWide], [sizeRep pixelsHigh])];
[sizeRep release];
[fullsizeData release];
}
This works fine until I put in too many images of too big
filesizes. Suddenly (in my above example at image no. 217) no
images will be loaded and the buffer-array will hold values like
(-94, -121, 106) or (-112, 114, -12). Sometimes even the
application's main menu gets screwed up, displaying nothing but
strange characters (its basically an "A" inside a square box).
This is consistent with imageData being NULL, and so getBytes does
nothing and buffer has whatever random garbage is on the stack.
This looks to me like some big memory desaster, but I have no idea
where to start looking...
Based on this snippet of code, you're not closing the file that was
opened when you did [[NSData alloc] initWithContentsOfFile:
thumbnailPath]. Do you have [imageData release]?
Also note, after fixing that, that the [NSBitmapImageRep
imageRepWithContentsOfFile: thumbnailPath] also will open the file,
and since you retain it and never release it, it will also leak a
file descriptor.
More subtly, once you fix that, imageRepWithContentsOfFile is
autoreleased, and won't actually be released until the next main
event loop is run, so if this code is inside a large loop (like
iterating through thousands of files), you'll want to add an
autorelease pool that your periodically empty explicitly...
Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
quadrium | build, mutate, evolve | images, textures, backgrounds, art
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden