From 1426edfebc5ee4108287afdc62ae90959f2dab61 Mon Sep 17 00:00:00 2001 From: arunjose696 Date: Thu, 31 Jul 2025 16:03:11 +0200 Subject: [PATCH] [Win32] Do not set IME font if the font is disposed #2323 Prior to recent changes that ensure that a font handle is always retrieved via the SWTFontProvider, the hFont variable in Caret::setIMEFont was retrieved directly from the font object (using font.handle). Since those recent changes, hFont is fetched via SWTFontProvider, which throws an exception if the font is disposed. Previously, when the font was disposed, hFont (font.handle) would be zero, and the method would fall back to using defaultFont for setting the IME font. Now, this fallback no longer works because the exception is thrown before the fallback can occur. This commit restores the intended behavior by setting hFont as zero if the font is disposed, preventing the exception. Fixes https://github.com/eclipse-platform/eclipse.platform.swt/issues/2323 --- .../org/eclipse/swt/internal/SWTFontProvider.java | 3 +++ .../junit/Test_org_eclipse_swt_widgets_Canvas.java | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java index 235cd48aafa..7312a5acb20 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/SWTFontProvider.java @@ -74,6 +74,9 @@ public static long getFontHandle(Font font, int zoom) { if (font == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } + if (font.isDisposed()) { + return 0; + } return Font.win32_getHandle(getFont(font.getDevice(), font.getFontData()[0], zoom)); } diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Canvas.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Canvas.java index 2b4e2315393..9eade51b27f 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Canvas.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Canvas.java @@ -108,6 +108,17 @@ public void test_setFontLorg_eclipse_swt_graphics_Font() { font.dispose(); } +@Test +public void test_CaretWithDisposedFontDoesNotThrowException_issue2323() { + Caret caret = new Caret(canvas, SWT.NONE); + Font font = new Font(canvas.getDisplay(), "Default", 10, SWT.BOLD); + shell.open(); + caret.setFont(font); + font.dispose(); + canvas.setFocus(); + canvas.setCaret(caret); +} + /* custom*/ @Test public void test_consistency_MenuDetect() {