2 messages in net.java.dev.jna.usersRe: [jna-users] Native memory managem...
FromSent OnAttachments
Buckel, WolfgangDec 19, 2008 6:13 am 
Timothy WallDec 19, 2008 7:58 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] Native memory management model of JNA?Actions...
From:Timothy Wall (twal@dev.java.net)
Date:Dec 19, 2008 7:58:24 am
List:net.java.dev.jna.users

On Dec 19, 2008, at 9:14 AM, Buckel, Wolfgang wrote:

Hello,

I tried out JNA with one of our DLLs and it worked pretty well. Thanks for the good work. However, I was wondering under which conditions JNA objects like Structure or primitive arrays take ownership of a block of native memory and consequently free that block of memory when GCed.

I could find this mail thread in the archives : https://jna.dev.java.net/servlets/ReadMsg?listName=users&msgNo=1777

That still does not fully answer my questions: - Does JNA free memory returned by functions (e.g. MyStruct* getNewStruct() )?

No. In these cases, you must free the memory yourself if the native API dictates it. The allocated Structure uses a Pointer rather than Memory; Pointer has no "free" semantics.

- How does it work with Struct* arguments passed into a Callback? Does JNA assume that this memory will be freed by the caller?

It is assumed that the caller owns the memory.

- How are the assumptions in general about who is responsible for freeing which memory?

JNA frees memory allocated by JNA (ie com.sun.jna.Memory). If your Java app needs to free memory allocated by native code, you'll need to map the appropriate "free" method and call it as appropriate.