Hot (well, thread-safe) or not?
Hot (well, thread-safe) or not?
- Subject: Hot (well, thread-safe) or not?
- From: John Pannell <email@hidden>
- Date: Sun, 17 Oct 2004 21:59:10 -0600
Hello list-
I have an application I've reached a dead end with. I have used documentation to eliminate leaky/crashy behavior, but I'm stuck with a spinny-cursor/hangy problem. I have used spin control on the app, and the issue seems to be thread contention (always a lot of semaphore_wait or mutex_wait going on when the app hangs). What follows is a block of code that creates a thumbnail in a thread (it is the only method in the app that is spun off in another thread). My question to the list... can anyone see any code which is not thread-safe?
Any thoughts or comments are much appreciated!
John
{
// thread safe
NSAutoreleasePool *thePool = [[NSAutoreleasePool alloc] init];
float scale, heightFactor, widthFactor;
NSImage *thumbImage = [[NSImage alloc] initWithSize:NSMakeSize(96, 96)];
NSBitmapImageRep *myJPEGRep;
NSData *thumbData;
IconFamily* iconFamily;
// parse incoming string into image number and storage location and thumb location
// locations (on disk) are guaranteed unique
NSArray *infoArray = [theURL componentsSeparatedByString:@"|"];
NSString *myStorageLocation = [infoArray objectAtIndex:1];
NSString *myThumbLocation = [infoArray objectAtIndex:2];
// get image from file on disk
NSData *data = [[NSData alloc] initWithContentsOfFile:myStorageLocation];
// create image object
NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc]
initWithData:data];
NSImage *theImage = [[NSImage alloc] initWithSize:[bitmapImageRep size]];
[theImage addRepresentation:bitmapImageRep];
[theImage setScalesWhenResized:YES];
[theImage setSize:NSMakeSize([bitmapImageRep pixelsWide], [bitmapImageRep pixelsHigh])];
// create thumbnail image for matrix
NSAffineTransform *at = [NSAffineTransform transform];
[theImage setScalesWhenResized:YES];
heightFactor = 96.0/[theImage size].height;
widthFactor = 96.0/[theImage size].width;
if(heightFactor > widthFactor){
scale = widthFactor;
} else {
scale = heightFactor;
}
[at scaleBy:scale];
[thumbImage lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationLow];
[theImage setSize:[at transformSize:[theImage size]]];
[theImage compositeToPoint:NSMakePoint((96-[theImage size].width)/2 , (96-[theImage size].height)/2) operation:NSCompositeCopy];
[thumbImage unlockFocus]; // this image is now an NSCachedImageRep
// create a compressed JPEG image to hand over - lower memory requirements (hopefully)
myJPEGRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[[[thumbImage representations] objectAtIndex:0] rect]];
[thumbImage addRepresentation:myJPEGRep];
// write to disk
thumbData = [[[thumbImage representations] objectAtIndex:1] representationUsingType:NSJPEGFileType properties:nil];
[thumbData writeToFile:myThumbLocation atomically:YES];
// put it in
// create dictionary of needed elements
NSArray *objects = [NSArray arrayWithObjects:myStorageLocation,myThumbLocation,nil];
NSArray *keys = [NSArray arrayWithObjects:nil];
NSDictionary *d = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
// send to main thread for inclusion in matrix
[self performSelectorOnMainThread:@selector(addImage:) withObject:d waitUntilDone:NO];
// clean up
[bitmapImageRep release];
[theImage release];
[thumbImage release];
[data release];
[myJPEGRep release];
// bye bye pool
[thePool release];
// kill thread (necessary?)
[NSThread exit];
} _______________________________________________
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