15 messages in net.java.dev.jna.usersRe: [jna-users] Override Pointer.fina...
FromSent OnAttachments
bb...@cox.netJun 26, 2007 9:11 am 
Timothy WallJun 26, 2007 12:52 pm 
Wayne MeissnerJun 26, 2007 7:43 pm 
Ben ChaseJun 26, 2007 8:39 pm 
Wayne MeissnerJun 26, 2007 10:33 pm 
Timothy WallJun 27, 2007 5:08 am 
Timothy WallJun 27, 2007 5:20 am 
Rick GoldsteinJun 27, 2007 8:50 am 
Timothy WallJun 27, 2007 9:59 am 
Ben ChaseJun 27, 2007 7:26 pm 
Timothy WallJun 28, 2007 3:56 am 
bb...@cox.netJun 28, 2007 4:43 am 
bb...@cox.netJun 28, 2007 5:37 am 
bb...@cox.netJun 28, 2007 6:18 am 
Wayne MeissnerJun 28, 2007 7:41 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] Override Pointer.finalize to reclaim Native resources?Actions...
From:Wayne Meissner (wmei@gmail.com)
Date:Jun 26, 2007 7:43:25 pm
List:net.java.dev.jna.users

Timothy Wall wrote:

I just get rid of pointers in a finally block, but that doesn't work well for long-lived pointers. Wayne Meissner has done some stuff with gstreamer, which uses a number of pointer objects of different types, although I think the v3 branch he's using allows derived pointer types. I'm not sure where he's keeping the gstreamer code he's working on, but I'm sure he'll chime in.

The gstreamer-java code is at http://code.google.com/p/gstreamer-java/

Basically what I do is treat the treat the raw gstreamer pointers as an opaque handle (since they really are), and use proxies to access them.

e.g. in your case:

class MediaObject { Pointer handle; public MediaObject() { PointerByReference phi = new PointerByReference(); media.createHandle(phi); handle = phi.getValue(); } public void readImage() { Media.INSTANCE.readImage(handle); } protected void finalize() { Media.INSTANCE.destroyHandle(handle); } }

I did this mainly because I wanted to put a more java-ish API over the raw gst_object_fiddle_with_bar() type API.

You can use the new TypeMapper API in JNA 2.4 to implement auto-conversion to/from proxy objects. So if you don't want to write wrapper functions for each method, like above, you can use an ArgumentConverter to automagically pass MediaObject.handle to the underlying API.

e.g.

interface Media { static final TypeMapper TYPE_MAPPER = new DefaultTypeMapper() {{ addArgumentConverter(MediaObject.class,new ArgumentConverter() { public Object toNative(Object obj) { return ((MediaObject)obj).handle; } }); }}; public static final Media INSTANCE = Native.loadLibrary("media", Media.class, new HashMap() {{ put(Library.OPTION_TYPE_MAPPER, TYPE_MAPPER); }}); public void readImage(MediaObject obj); public void frobImage(MediaObject obj); }

You should then be able to do MediaObject obj = new MediaObject(); Media.INSTANCE.readImage(obj); Media.INSTANCE.frobImage(obj);

It might make sense to have a protected copy constructor for Pointer, so that given a Pointer object, you can create your custom type given the original as an argument. Then you could use the type mapping system to convert the generic Pointer types to your desired type.

I think a copy constructor is definitely needed. Also a way to get the raw pointer value as a long could be useful in some cases.

I'm not sure having non-public ctor/getters on Pointer really buys that much safety - after all, JNA already lets people do pretty dangerous things, and you can bypass it using malloc. e.g. Pointer p = libc.malloc(4); p.setInt(0, 0xdeadbeef); p = p.getPointer(0); // p now points to 0xdeadbeef