Should I free the pointer returned by native method manually?
tison
wander4096 at gmail.com
Mon Jan 22 18:28:50 UTC 2024
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