Question about global SegmentScope
Jorn Vernee
jorn.vernee at oracle.com
Mon Apr 17 07:25:54 UTC 2023
I think they are asking not about the library symbols, but about the
pointer returned by OPENSSL_version?
Whether such pointers should be 'freed' depends on the native library.
Some native libraries might allocate memory and return a pointer to the
user. It is up to the native library to specify how the memory should be
freed in that case. Usually if the user is expected to free the memory a
special function to do that is provided by the library.
In this case, OPENSSL_version seems to be returning a pointer to a
constant string (string literal), which doesn't have to be freed by the
user.
Jorn
On 16/04/2023 14:32, Glavo wrote:
> The zero-length MemorySegment associated with the global scope is a
> wrapper of the raw pointer.
> It indicates that the lifetime is unknown, rather than indicating that
> the lifetime is truly infinite.
>
> The true lifetime of these symbols is associated with SymbolLookup.
> They are also invalid when the scope of the SymbolLookup is invalid.
>
> Glavo
>
> On Sun, Apr 16, 2023 at 7:52 PM 刘希晨 <benrush0705 at gmail.com> wrote:
>
> Thank you for the clarification. I didn' mean the SymbolLookup
> itself, but the MemorySegment that MethodHandle returns, the
> MethodHandle is created by , I already tested no matter what kind
> of SegmentScope SymbolLookup used, the native MemorySegment that
> MethodHandle returns would always be Global scope.
>
> A example would be like below :
>
> ```
> SymbolLookup crypto =
> SymbolLookup.libraryLookup(Path.of("C:\\Program
> Files\\OpenSSL-Win64\\libcrypto-3-x64.dll"), SegmentScope.auto());
> MemorySegment methodPointer = crypto.find("OPENSSL_version")
> .orElseThrow(() -> new RuntimeException("Unable to load target
> native method")); MethodHandle openSSLVersion =
> Linker.nativeLinker().downcallHandle(methodPointer,
> FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_INT));
> MemorySegment version = (MemorySegment)
> openSSLVersion.invokeExact(0); System.out.println(version);
> ```
> the version Segment is global somehow and I don't know how to
> release it
>
> Glavo <zjx001202 at gmail.com> 于2023年4月16日周日 19:38写道:
>
> SymbolLookup.libraryLookup takes a SegmentScope as a
> parameter, which represents the lifecycle of the returned
> SymbolLookup.
>
> It looks like you are using an old JDK? Panama has critical
> changes every Java update, please use the latest JDK.
>
> Glavo
>
> On Sun, Apr 16, 2023 at 6:36 PM 刘希晨 <benrush0705 at gmail.com>
> wrote:
>
> Hi I have a question about global segment scope.
>
> If I use SymbolLookup.libraryLookup to load a C library
> and call its function which returns a pointer , in java
> it's represented by a memory segment, the memorysegment's
> scope would be global scope. Does this mean that the
> memory segment object wouldn't be released by anything
> until the VM exits?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20230417/cab62fd4/attachment-0001.htm>
More information about the panama-dev
mailing list