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.