<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>I think they are asking not about the library symbols, but about
the pointer returned by OPENSSL_version?<br>
<br>
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.<br>
</p>
<p>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.<br>
</p>
<p>Jorn<br>
</p>
<div class="moz-cite-prefix">On 16/04/2023 14:32, Glavo wrote:<br>
</div>
<blockquote type="cite" cite="mid:CAJL5A3nvNvH=mXEHN1bVeMXh+5ng5XOAuGrAQxgmGUoznZB2Hg@mail.gmail.com">
<div dir="ltr">The zero-length MemorySegment associated with the
global scope is a wrapper of the raw pointer.<br>
<div>It indicates that the lifetime is unknown, rather than
indicating that the lifetime is truly infinite.<br>
</div>
<div><br>
</div>
<div>The true lifetime of these symbols is associated with
SymbolLookup. They are also invalid when the scope of the
SymbolLookup is invalid.<br>
</div>
<div><br>
</div>
<div>Glavo</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Sun, Apr 16, 2023 at
7:52 PM 刘希晨 <<a href="mailto:benrush0705@gmail.com" moz-do-not-send="true" class="moz-txt-link-freetext">benrush0705@gmail.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div dir="ltr">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.<br>
<div><br>
</div>
<div>A example would be like below :</div>
<div><br>
</div>
<div>```</div>
<div>
<div style="background-color:rgb(30,31,34);color:rgb(188,190,196);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre-wrap">SymbolLookup crypto = SymbolLookup.<span style="font-style:italic">libraryLookup</span>(Path.<span style="font-style:italic">of</span>(<span style="color:rgb(106,171,115)">"C:</span><span style="color:rgb(207,142,109)">\\</span><span style="color:rgb(106,171,115)">Program Files</span><span style="color:rgb(207,142,109)">\\</span><span style="color:rgb(106,171,115)">OpenSSL-Win64</span><span style="color:rgb(207,142,109)">\\</span><span style="color:rgb(106,171,115)">libcrypto-3-x64.dll"</span>), SegmentScope.<span style="font-style:italic">auto</span>());
MemorySegment methodPointer = crypto.find(<span style="color:rgb(106,171,115)">"OPENSSL_version"</span>)
.orElseThrow(() -> <span style="color:rgb(207,142,109)">new </span>RuntimeException(<span style="color:rgb(106,171,115)">"Unable to load target native method"</span>));
MethodHandle openSSLVersion = Linker.<span style="font-style:italic">nativeLinker</span>().downcallHandle(methodPointer, FunctionDescriptor.<span style="font-style:italic">of</span>(ValueLayout.<span style="color:rgb(199,125,187);font-style:italic">ADDRESS</span>, ValueLayout.<span style="color:rgb(199,125,187);font-style:italic">JAVA_INT</span>));
MemorySegment version = (MemorySegment) openSSLVersion.invokeExact(<span style="color:rgb(42,172,184)">0</span>);
System.<span style="color:rgb(199,125,187);font-style:italic">out</span>.println(version);</div>
</div>
<div>```</div>
<div>the version Segment is global somehow and I don't know
how to release it</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">Glavo <<a href="mailto:zjx001202@gmail.com" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">zjx001202@gmail.com</a>>
于2023年4月16日周日 19:38写道:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div dir="ltr">SymbolLookup.libraryLookup takes a
SegmentScope as a parameter, which represents the
lifecycle of the returned SymbolLookup.
<div><br>
</div>
<div>It looks like you are using an old JDK? Panama has
critical changes every Java update, please use the
latest JDK.<br>
<div><br>
</div>
<div>Glavo</div>
</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Sun, Apr 16, 2023
at 6:36 PM 刘希晨 <<a href="mailto:benrush0705@gmail.com" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">benrush0705@gmail.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px
0px 0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div dir="ltr">Hi I have a question about global
segment scope.
<div><br>
</div>
<div>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?</div>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</body>
</html>