Problem with NSImage in JNI

  • 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;
    }
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today