0

I have a JNA interface like this:


interface JJ {
    String Hello(GoString.ByValue sql);
}

Corresponding native code in Go:


//export Hello
func Hello(ss string) *C.char {

    s := ss + " world"
    return C.CString(s)
}

The native code returns a pointer to a string.

Does JNA code free the pointer of the string allocated by the native code? If not, how to free it?

0

2 Answers 2

1

I'll answer both the generic question and the specific example.

JNA doesn't maintain any references to the native memory unless you allocate it yourself in JNA (e.g., define a byte[] array or Memory buffer that you pass to a function). In these cases, the native memory is freed when the Java object is garbage collected.

If you're not passing any memory to C to fill, JNA isn't going to do anything with the native memory, and you'd have to read the API documentation to see what your responsibility is for freeing the native string.

The C++ CString type isn't necessarily required to be freed unless it is stored in a new object. However, Go does implement the CString as an object, and documents these requirements. For your particular example, the docs say:

Memory allocations made by C code are not known to Go's memory manager. When you create a C string with C.CString (or any C memory allocation) you must remember to free the memory when you're done with it by calling C.free.

and from the cgo wiki:

One important thing to remember is that C.CString() will allocate a new string of the appropriate length, and return it. That means the C string is not going to be garbage collected and it is up to you to free it. A standard way to do this follows.

// #include <stdlib.h>

import "C"
import "unsafe"
...
    var cmsg *C.char = C.CString("hi")
    defer C.free(unsafe.Pointer(cmsg))
    // do something with the C string

Of course, you aren't required to use defer to call C.free(). You can free the C string whenever you like, but it is your responsibility to make sure it happens.

2
  • 1
    as docs say CString copies and allocates a new string which needs to be freed explicitly, so its not allocated on stack infact
    – pdeva
    Nov 8, 2019 at 18:37
  • @pdeva thanks, I've updated my answer. I don't know if that's a peculiarity with Go. I based my answer on this answer. It may be that Go internally calls the new variant. Nov 8, 2019 at 19:12
1

Get Java to allocate the buffer so it will free it for you instead.. otherwise you MUST free it yourself and export deallocate functions..

public interface JJ {
    JJ lib = (JJ)Native.loadLibrary("jj.so", JJ.class);

    void Hello(byte[] sql);
}

func Hello(ss []byte) {
    copy(ss[:], string(ss) + " world")
}

Usage:

void main(String[] args) {
    byte[] text = new byte[256];

    JJ.lib.Hello(text);

    System.out.println(Native.toString(text));
}

You can wrap the entire thing into a generic function that will create a string for you for any given native function.. but you get the idea.

  1. Java allocates buffer, Go-Lang fills it, Java deallocates it automatically.
  2. Go-Lang allocates buffer, Java uses it, Go-Lang frees it.

In both cases, whoever allocates the buffer must free it.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.