[foreign-jextract] Closing sliced segment closes original segment

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue Aug 4 13:45:02 UTC 2020


Filed:

https://bugs.openjdk.java.net/browse/JDK-8251049

Maurizio

On 04/08/2020 14:19, Maurizio Cimadamore wrote:
>
> On 04/08/2020 14:06, Filip Krakowski wrote:
>> Hi,
>>
>> I had a couple of IllegalStateExceptions stating that the segment I 
>> tried to close was already closed. After some debugging I found out 
>> this is caused by using a try-with-resources statement in combination 
>> with a MemorySegment (slice) returned by a field access ( try(var 
>> slice = struct.field$addr(originalsegment) { ... } ). After the 
>> statement the slice gets closed which also happens to close the 
>> original MemorySegment. I find this very confusing and would like to 
>> ask if this is intended behaviour or a bug.
>
> This is the intended behavior, and is documented in the API javadoc.
>
> When using the memory segment API directly, you can protect against 
> this (if you so wish) by making the sliced segment not closeable (see 
> MemorySegment::withAccessModes), before sharing it with a client.
>
> In this case the offending party is jextract - so I think we'll have 
> to do something in the extracted code to prevent this issue e.g. have 
> jextract mark the returned slice as non-closeable, to make it clear 
> that in no way the returned segment can have lifecycle side-effects on 
> the original segment.
>
> Thanks
> Maurizio
>
>>
>> Best regards,
>> Filip
>> Email Signature


More information about the panama-dev mailing list