Should I free the pointer returned by native method manually?

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Jan 22 18:36:09 UTC 2024


Hi tison,
in this case, the function is returning a pointer. What happens to that 
pointer is 100% library-dependent. In this case, doing `man getpwnam` 
gives us some clues:

> The return value may point to a static area, and may be overwritten  by
>        subsequent  calls  to  getpwent(3), getpwnam(), or getpwuid().  
> (Do not
>        pass the returned pointer to free(3).)

Basically, the returned pointer points to an area that is managed by the 
library, so you don't have to free.

This means the code you have seems correct to me.

Other libraries will behave differently, for instance they might have a 
pair of functions such as "createFoo" and "destroyFoo", the former 
allocating region of memory, and the latter destroying such region. In 
such cases it might be helpful to associate the return memory segment to 
an existing arena *and* also attach a cleanup function (via 
MemorySegment::reinterpret), so that, when the arena is closed, 
"destroyFoo" is called on the pointer.

Hope this helps

Maurizio


On 22/01/2024 18:28, tison wrote:
> Here is the code snippet:
>
>      public static void main(String[] args) throws Throwable {
>          final Linker linker = Linker.nativeLinker();
>          final SymbolLookup libc = linker.defaultLookup();
>          final MethodHandle handle =
> linker.downcallHandle(libc.find("getpwnam").orElseThrow(),
> FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS));
>          try (Arena arena = Arena.ofConfined()) {
>              final MemorySegment passwd = (MemorySegment)
> handle.invoke(arena.allocateUtf8String("tison"));
>              System.out.println("passwd=" + passwd);
>              System.out.println("pw_name=" +
> passwd.reinterpret(Long.MAX_VALUE).get(ValueLayout.ADDRESS,
> 48).reinterpret(Long.MAX_VALUE).getUtf8String(0));
>          }
>      }
>
> I wonder if I should explicitly free passwd that is returned from a
> native method. If so, how? If not, how the Arena tracks it and free on
> closed?
>
> Best,
> tison.


More information about the panama-dev mailing list