RFR: 8371128: NullPointerException occurs due to double cleanup of SwingNode
Kevin Rushforth
kcr at openjdk.org
Tue Nov 4 16:55:39 UTC 2025
On Tue, 4 Nov 2025 09:06:41 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:
> It is seen that when we close the SwingNode window while we have registered a WINDOW_HIDDEN listener, it will cause a NPE
> This is because WINDOW_HIDDEN listener called `SwingNode.overrideNativeWindowHandle` to provide a native FX window handle to AWT/Swing via JNI code, which gives provision to AWT/Swing code in sun/swing/JLightweightFrame.java#overrideNativeWindowHandle to handle the window closing
>
> https://github.com/openjdk/jdk/blob/e4aed95cac343f1339b9bc87721561bdc4c2f5ad/src/java.desktop/share/classes/sun/swing/JLightweightFrame.java#L527-L534
>
> But if window is closed, then the node is removed from scene and `SwingNode.disposeLwFrame` is called which nulls "lwFrame" which is the handle for JLightweightFrame AWT frame, so when JNI code
> https://github.com/openjdk/jfx/blob/c77c2335856a967907aaacc9546f44943c069add/modules/javafx.graphics/src/main/native-prism/SwingInterop.c#L47
>
> tries to call JLightweightFrame, it crashes.
> Fix is made to check for `lwFrame `before `overrideNativeWindowHandle ` is called inside `SwingNodeHelper.runOnEDT`.
> This is common practice employed inside `SwingNodeHelper.runOnEDT` to check for lwFrame to be not null
LGTM. The fix is safe and obvious. I left one minor comment on the test and will reapprove if you decide to make any changes.
This fix is simple enough that a single reviewer should suffice. Wait until it has been out for review for at least 24 hours before integrating.
tests/manual/swing/JavaSwingNodeCleanupBug.java line 57:
> 55: passButton.setOnAction(e -> this.quit());
> 56: failButton.setOnAction(e -> {
> 57: this.quit();
Minor: Maybe throw an exception or print an error message here?
-------------
Marked as reviewed by kcr (Lead).
PR Review: https://git.openjdk.org/jfx/pull/1960#pullrequestreview-3417573997
PR Review Comment: https://git.openjdk.org/jfx/pull/1960#discussion_r2491289190
More information about the openjfx-dev
mailing list