Question about global SegmentScope

Glavo zjx001202 at gmail.com
Sun Apr 16 12:32:39 UTC 2023


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/20230416/dbd36e5a/attachment.htm>


More information about the panama-dev mailing list