On Tue, 28 Jul 2026 21:22:22 GMT, Michael Strauß <[email protected]> wrote:

> On Windows, changing a system preference such as the light/dark color mode 
> can crash an application that embeds JavaFX in SWT using `FXCanvas`.
> 
> `GlassApplication` receives the Java `WinApplication` instance as the local 
> JNI reference `jrefThis`. It passes this reference to `PlatformSupport` and 
> separately creates a global reference for `m_grefThis`:
> 
> GlassApplication::GlassApplication(jobject jrefThis) : BaseWnd(), 
> m_platformSupport(GetEnv(), jrefThis)
> {
>     m_grefThis = GetEnv()->NewGlobalRef(jrefThis);
>     ...
> }
> 
> 
> Creating `m_grefThis` does not promote the original reference. `NewGlobalRef` 
> returns a new handle, while the copy retained by `PlatformSupport` remains a 
> local JNI reference. That local reference becomes invalid when the native 
> method invocation returns.
> 
> Using an invalid JNI reference is undefined behavior. Depending on the state 
> of the reused local-reference slot, the result can be a 
> `NullPointerException` without Java frames or an access violation inside the 
> JVM.
> 
> This change stores the application reference as a `JGlobalRef<jobject>`, 
> which creates an independent global JNI reference while the input local 
> reference is still valid.
> 
> ---------
> - [x] I confirm that I make this contribution in accordance with the [OpenJDK 
> Interim AI Policy](https://openjdk.org/legal/ai).

LGTM, tested on my Windows 11 machine, usual tests also look good

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

Marked as reviewed by lkostyra (Reviewer).

PR Review: https://git.openjdk.org/jfx/pull/2226#pullrequestreview-4830380251

Reply via email to