12 messages in net.java.dev.jna.usersRe: [jna-users] Problem returning Str...
FromSent OnAttachments
Duncan McGregorFeb 9, 2009 2:13 pm 
Timothy WallFeb 9, 2009 2:24 pm 
Timothy WallFeb 9, 2009 2:31 pm 
Duncan McGregorFeb 9, 2009 4:22 pm 
Timothy WallFeb 9, 2009 4:57 pm 
Duncan McGregorFeb 9, 2009 5:39 pm 
Timothy WallFeb 9, 2009 5:48 pm 
Timothy WallFeb 9, 2009 6:00 pm 
Timothy WallFeb 9, 2009 6:07 pm 
Duncan McGregorFeb 9, 2009 6:10 pm 
Duncan McGregorFeb 10, 2009 2:53 am 
Duncan McGregorFeb 10, 2009 7:42 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Problem returning Structure.ByValue on Mac, JNA 3.0.6+Actions...
From:Timothy Wall (twal@dev.java.net)
Date:Feb 9, 2009 4:57:57 pm
List:net.java.dev.jna.users

On Feb 9, 2009, at 7:22 PM, Duncan McGregor wrote:

On 9 Feb 2009, at 22:31, Timothy Wall wrote:

It'd also be helpful if you could put this into a standalone test.

OK, although it makes it clear that Rococoa does add value!

Actually, I was thinking of using the struct by value in a comparable situation exclusive of the OSX framework, thus eliminating improper usage or interaction with the framework as a contributing factor.

import junit.framework.TestCase;

import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.NativeLong; import com.sun.jna.Structure;

public class StructsInObjectsLowlevelTest extends TestCase {

public static class NSSize extends Structure implements Structure.ByValue { public float width; public float height;

public NSSize() {}

public NSSize(float width, float height) { this.width = width; this.height = height; } }

public interface FoundationLibrary extends Library { void NSLog(NativeLong pString, Object thing);

NativeLong CFStringCreateWithCString(NativeLong allocator, String string, int encoding);

NativeLong objc_getClass(String className); NativeLong class_createInstance(NativeLong pClass, int extraBytes); Selector sel_registerName(String selectorName);

NativeLong objc_msgSend(NativeLong receiver, Selector selector, Object... args); }

// Required to fix stack for NSSize public interface NSSizeHackLibrary extends Library { NSSize objc_msgSend(NativeLong receiver, Selector selector, Object... args); }

public void test() { FoundationLibrary library = (FoundationLibrary) Native.loadLibrary("Foundation", FoundationLibrary.class); NativeLong logTemplate = library.CFStringCreateWithCString(new NativeLong(0), "%@", 0);

NativeLong idOfNSValueClass = library.objc_getClass("NSValue"); library.NSLog(logTemplate, idOfNSValueClass);

NSSize aSize = new NSSize(1, 3); NativeLong idOfValueInstance = library.objc_msgSend( idOfNSValueClass, library.sel_registerName("valueWithSize:"), aSize); library.NSLog(logTemplate, idOfValueInstance);

NSSizeHackLibrary library2 = (NSSizeHackLibrary) Native.loadLibrary("Foundation", NSSizeHackLibrary.class); NSSize size = library2.objc_msgSend(idOfValueInstance, library.sel_registerName("sizeValue"));

assertEquals(1.0, size.width, 0.0001); assertEquals(3.0, size.height, 0.0001); }

}

Seems to be just missing "Selector"...

Run with JNA 3.0.5 this test passes, but crashes with 3.0.6 (and, as I found accidentally, with 3.0.5 if you link the 3.0.6 jnilib)

2009-02-10 00:15:07.612 java[49021:1003] NSValue 2009-02-10 00:15:07.626 java[49021:1003] *** _NSAutoreleaseNoPool(): Object 0x11cdf0 of class NSConcreteValue autoreleased with no pool in place - just leaking Stack: (0x9134f73f 0x9125be32 0x912d86f6 0x61844fd 0x61841b9 0x61781be 0x617881b 0x35859b1) 2009-02-10 00:15:07.628 java[49021:1003] NSSize: {1, 3} Invalid memory access of location 00000020 eip=95afb688

Is the autoreleased object the one being accessed?