Problem with NSImage in JNI
Problem with NSImage in JNI
- Subject: Problem with NSImage in JNI
- From: Daniel Dalquen <email@hidden>
- Date: Tue, 30 Oct 2007 22:49:48 +0100
Hello everybody!
I have a problem with some code for a Java native interface that gets
the icon for a specific file type from NSWorkspace and returns it to
the calling Java method (see code below). Everything works fine on
10.4, but on Leopard I get the following error message when the
function is called and the Java app crashes:
"HIToolbox: ignoring exception 'Error (1002) creating CGSWindow' that
raised inside Carbon event dispatch
java(1573,0xa0876074) malloc: *** error for object 0x21000: pointer
being freed was not allocated"
I have found the statement that causes the crash, but don't really
know what goes wrong (as I said, it works on Tiger). The Java app uses
SWT for the GUI (which in turn uses Carbon afaik).
Any idea is appreciated :)
Cheers,
Daniel
JNIEXPORT jbyteArray JNICALL
Java_com_wuala_platform_JNIWrapper_getIcon(JNIEnv *env, jclass cls,
jstring arg, jint iconsize)
{
// convert java string to UTF8 encoding
const char *argutf = (*env)->GetStringUTFChars(env, arg, JNI_FALSE);
// create Cocoa string
NSString * path = [NSString stringWithUTF8String:argutf];
// ask the default workspace for the icon for the extension we want
or a folder icon
NSImage * iconimage;
if ([path isEqualToString:@"/usr/"])
iconimage = [[NSWorkspace sharedWorkspace] iconForFile:path];
else
iconimage = [[NSWorkspace sharedWorkspace] iconForFileType:path];
// define and set the output size
NSSize s = NSMakeSize(iconsize, iconsize);
[iconimage setSize:s];
// release the UTF8 string
(*env)->ReleaseStringUTFChars(env, arg, argutf);
// the icon that NSWorkspace returns is masked. In order for Java to
be able to use it,
// we must remove the transparency by compositing the icon onto a
white background
NSSize size = [iconimage size];
NSImage *image2 = [[NSImage alloc] initWithSize:size];
[image2 lockFocus]; //Crashes here in Leopard
[[NSColor whiteColor] set];
[NSBezierPath fillRect: NSMakeRect (0.0, 0.0, size.width,
size.height)];
[iconimage compositeToPoint:NSMakePoint(0.0, 0.0)
operation:NSCompositeSourceOver fraction:1.0];
[image2 unlockFocus];
// get the TIFF data of the image
NSData * imageData = [image2 TIFFRepresentation];
unsigned length = [imageData length];
// initialize a java byte array in which we will then copy the TIFF
data
jbyteArray result = 0;
result = (*env)->NewByteArray(env,length);
if (result != NULL) {
(*env)->SetByteArrayRegion(env,result,0,length,(jbyte *)[imageData
bytes]);
}
[image2 release];
return result;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden