Skip to content

Commit 716e80e

Browse files
ptzieglerakurtakov
authored andcommitted
[GTK] Fix NPE in GC if already disposed
Calling `stringExtent(...)` or `textExtent(...)` may throw a NullPointerException, rather than an SWTException if the GC object has already been disposed. This is unexpected and inconsistent with how GC behaves on other platforms.
1 parent cc104be commit 716e80e

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/GC.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3786,6 +3786,7 @@ public void setXORMode(boolean xor) {
37863786
* </ul>
37873787
*/
37883788
public Point stringExtent(String string) {
3789+
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
37893790
return textExtentInPixels(string, 0);
37903791
}
37913792
Point stringExtentInPixels(String string) {
@@ -3812,6 +3813,7 @@ Point stringExtentInPixels(String string) {
38123813
* </ul>
38133814
*/
38143815
public Point textExtent(String string) {
3816+
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
38153817
return textExtentInPixels(string, SWT.DRAW_DELIMITER | SWT.DRAW_TAB);
38163818
}
38173819

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.concurrent.atomic.AtomicReference;
3636

3737
import org.eclipse.swt.SWT;
38+
import org.eclipse.swt.SWTException;
3839
import org.eclipse.swt.graphics.Color;
3940
import org.eclipse.swt.graphics.Font;
4041
import org.eclipse.swt.graphics.FontMetrics;
@@ -917,20 +918,29 @@ public void test_stringExtentLjava_lang_String() {
917918
Point pt = gc.stringExtent("abc");
918919
assertTrue(pt.x > 0);
919920
assertTrue(pt.y > 0);
921+
gc.dispose();
922+
SWTException e = assertThrows(SWTException.class, () -> gc.stringExtent("abc"));
923+
assertEquals(SWT.ERROR_GRAPHIC_DISPOSED, e.code);
920924
}
921925

922926
@Test
923927
public void test_textExtentLjava_lang_String() {
924928
Point pt = gc.textExtent("abc");
925929
assertTrue(pt.x > 0);
926930
assertTrue(pt.y > 0);
931+
gc.dispose();
932+
SWTException e = assertThrows(SWTException.class, () -> gc.textExtent("abc"));
933+
assertEquals(SWT.ERROR_GRAPHIC_DISPOSED, e.code);
927934
}
928935

929936
@Test
930937
public void test_textExtentLjava_lang_StringI() {
931938
Point pt = gc.textExtent("abc", 0);
932939
assertTrue(pt.x > 0);
933940
assertTrue(pt.y > 0);
941+
gc.dispose();
942+
SWTException e = assertThrows(SWTException.class, () -> gc.textExtent("abc", 0));
943+
assertEquals(SWT.ERROR_GRAPHIC_DISPOSED, e.code);
934944
}
935945

936946
@Test

0 commit comments

Comments
 (0)