
Hi Claudio, Thanks for you informing about new JNA version. I try to use JNA provided API. It works well. The updated code is as following: public SchedParameter[] getSchedulerParameters() throws LibvirtException { IntByReference nParams = new IntByReference(); SchedParameter[] returnValue = new SchedParameter[0]; - String scheduler = libvirt.virDomainGetSchedulerType(VDP, nParams); + Pointer pScheduler = libvirt.virDomainGetSchedulerType(VDP, + nParams); processError(); - if (scheduler != null) { + if (pScheduler != null) { + String scheduler = pScheduler.getString(0); + Native.free(Pointer.nativeValue(pScheduler)); virSchedParameter[] nativeParams = new virSchedParameter[nParams.getValue()]; returnValue = new SchedParameter[nParams.getValue()]; libvirt.virDomainGetSchedulerParameters(VDP, nativeParams, nParams); If there is no issue. I recommend to use this solution to enhance all JNA code. BTW: Not all the returned String should be freed by JNA. For example, In Domain Class, the result returned by the method getName/ getUUIDString can't be freed. Because the reference were not allocated temporarily. We must analyze case by case. B.R. Benjamin Wang -----Original Message----- From: Claudio Bley [mailto:cbley@av-test.de] Sent: 2012年10月11日 23:36 To: Benjamin Wang (gendwang) Cc: veillard@redhat.com; libvir-list@redhat.com; Yang Zhou (yangzho) Subject: Re: [libvirt] Memory free in libvirt JNA At Thu, 11 Oct 2012 08:37:23 +0000, Benjamin Wang (gendwang) wrote:
Hi Claudio, Sorry for my late response. I have gone through Claudio's solution. It's good. But I think this is not a common solution. There are two points: 1. This solution must use Pointerbyreference to encapsulate the Pointer. This is not clean.
Yes, as I said, this adds another level of indirection --- which is pretty useless in Java.
2. Libvirt provides virFree method. But a common library could not provide memory management functions.
Sorry, I don't get your point here.
My proposal is as following: 1. Add a new Class Libc.java public interface Libc extends Library{ Libc INSTANCE = (Libc) Native.loadLibrary("c", Libc.class);
public void free(Pointer p); }
Not every platform has a shared library called "c". On Windows this would be msvcrt.dll for the Microsoft runtime. So, you would need to branch on the platform to load whatever library seems appropriate. Also, I just discovered that since version 3.3.0 JNA provides a public free method itself. Since I get crashes when using callback functions with JNA 3.2.7 in certain circumstances it is better just to require a newer version of JNA, IMHO. I'll post a few patches with improvements and memory fixes tomorrow. -- AV-Test GmbH, Henricistraße 20, 04155 Leipzig, Germany Phone: +49 341 265 310 19 Web:<http://www.av-test.org> Eingetragen am / Registered at: Amtsgericht Stendal (HRB 114076) Geschaeftsfuehrer (CEO): Andreas Marx, Guido Habicht, Maik Morgenstern