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:Ben Chase (bb@cox.net)
Date:Jun 26, 2007 8:39:39 pm
List:net.java.dev.jna.users

Timothy, Wayne, thanks for the help. I think I figured out another way to do it, after Timothy said something about copy constructors. Here it is.

package com.sun.jna;

import blah.Media.*;

public class ImageHandle extends Pointer { static Media media = Media.INSTANCE; protected void finalize() { Media.INSTANCE.destroyImageHandle(this); }

public ImageHandle(Pointer p) { peer = p.peer; }

public native ImageHandle getPointer(int offset); }

The fruitiest part of the above is that I had to make my code part of package com.sun.jna, in order to subclass. That was because of JNA constructors and/or fields marked "package private", in particular the ctors mentioned previously. So, if this technique ends up working, can we get the necessary bits changed from "package private" to "protected"? Because I feel strange claiming that my subclass is part of com.sun.jna.

So, given the above, at point of use, as soon as I get a Pointer (from PointerByReference.getVal()), I need to convert it to an ImageHandle using the above ctor. Not as elegant as what Wayne wrote, I guess. (Uh, I confess that I'm still trying to figure out exactly what Wayne wrote.)

In addition to having these ImageHandle objects automatically destroyed (yay!), the other benefit is a bit of type safety. The reality of my program is that there are not just ImageHandles, but already about four different kinds of handles coming out of the .dll, and there promises to be many more kinds of handles in the future as the program gets fleshed out. So it's nice to avoid having all these different kinds of handles all being just type Pointer and getting accidentally jumbled and causing mysterious crashes down in the .dll.

I've only given the above code a very cursory try and am at home now where I can't try it easily, so it might have a few warts yet. And without referring back to the JNA source, I'm trying to remember why I needed that getPointer() method. Must have been a good reason.

Anyhow, this JNA stuff is turning out really well. Great job, guys!

Thanks, Ben