resource scopes and close actions

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Feb 2 10:50:47 UTC 2022


Hi Michael,
I understand the concern. Running close actions _after_ the scope is 
closed (and so segments associated with it are inaccessible) is a sort 
of a forced move: you need to run the close actions when you are 100% 
sure that nobody else is accessing the scope. Otherwise you might have a 
race between a close action attempting to free some resource and another 
thread attempting to access it.

The result is that, as you have noticed, adding close actions is mostly 
useful when working with APIs that turn raw MemoryAddress into segments 
manually (MemorySegment::ofAddress).

Can you please share a problematic case with ffmpeg?

Thanks
Maurizio

On 02/02/2022 01:34, Michael Zucchi wrote:
>
> Morning,
>
> I was hoping to use resource scopes to auto-cleanup native-allocated 
> resources, but it looks like it's going to be a bit messy because 
> segments get closed before their close actions are invoked.
>
> Is this the intended behaviour or a side-effect of 'no particular order'?
>
> For some api's it doesn't matter as they don't use api-allocated 
> public structures (e.g. opencl) and the same approach is fine using 
> MemoryAddress, but for others it's an issue (e.g. ffmpeg) where the 
> obvious mapping is to use a segment and varhandles.
>
> Probably as a workaround - if i want to have this feature at any rate 
> because i'm not sure if it's a good idea yet - i'll just save a copy 
> of the address before it's turned into a segment and pass that to any 
> close function instead.
>
> I've only really started looking at seeing how to 'scope' things 
> 'properly' but it feels a bit clumsy and sort of not worth it because 
> it has to be worked around all the time either with hacks like this or 
> resorting to simply using the global scope which effectively does 
> nothing.
>
>  Z
>
>


More information about the panama-dev mailing list