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).

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

Commit messages:
 - Use global reference to application instance

Changes: https://git.openjdk.org/jfx/pull/2226/files
  Webrev: https://webrevs.openjdk.org/?repo=jfx&pr=2226&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8388526
  Stats: 2 lines in 1 file changed: 0 ins; 0 del; 2 mod
  Patch: https://git.openjdk.org/jfx/pull/2226.diff
  Fetch: git fetch https://git.openjdk.org/jfx.git pull/2226/head:pull/2226

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

Reply via email to