On Mon, 2 Jan 2023 09:26:14 GMT, Florian Kirmaier <fkirma...@openjdk.org> wrote:

>> This PR fixes the leak in the mac system menu bar.
>> 
>> Inside the native code, NewGlobalRef is called for the callable.
>> Which makes it into a "GC-Root" until DeleteGlobalRef is called.
>> 
>> The DeleteGlobalRef is never called for the MenuEntry, if it's removed from 
>> the menu without removing it's callable.
>> This PR adds logic, whether the Menu is inserted. If it's not inserted in a 
>> Menu anymore, then DeleteGlobalRef is called, by calling `_setCallback` with 
>> the callable "null".
>> 
>> The unit test verifies, that this bug happened without this change, but no 
>> longer happens with this change.
>
> Florian Kirmaier has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   JDK-8299423
>   Simplified the fix for the mac-menu-bar based on code review

If you're going forward with weak global references, you need to be aware that 
objects only referred to by weak references can be collected at any time. In 
order to do anything meaningful with a weak global reference, you need to 
create a new local reference to it and check whether the local reference is 
null (and don't forget to delete the local reference after using it).

Note that a local reference is automatically created for any object passed to a 
JNI method, so you only need to do that manually when the callback is used in 
other places.

-------------

PR: https://git.openjdk.org/jfx/pull/987

Reply via email to