Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 376478

Summary: [GC] textExtent can cause SWTException with "graphic disposed" message
Product: [RT] RAP Reporter: Victor Kirhenshtein <victor>
Component: RWTAssignee: Project Inbox <rap.incubator-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 1.4   
Target Milestone: 1.5 M7   
Hardware: All   
OS: All   
Whiteboard:

Description Victor Kirhenshtein CLA 2012-04-11 07:19:53 EDT
Build Identifier: 1.5.0-M6-20120320-1638

TextUtilities.getTextExtents can cause SWTException with "graphic disposed" message if font passed to it is equal to previously passed font, but previous font already disposed. This is caused by FigureUtilities.getTextDimension method, which looks like this:

protected static org.eclipse.swt.graphics.Point getTextDimension(String s,
		Font f) {
	setFont(f);
	return getGC().textExtent(s);
}

setFont looks like this:

protected static void setFont(Font f) {
	if (appliedFont == f || f.equals(appliedFont))
		return;
	getGC().setFont(f);
	appliedFont = f;
	metrics = null;
}

because Font.equals does not count disposed field in comparison, setFont keeps disposed font for calculations, which later causes SWTException.

This can be easily fixed either by changing conditin in setFont to

if ((appliedFont == f || f.equals(appliedFont)) && !appliedFont.isDisposed())

or by changing Font.equals method to take disposed flag into consideration.




Reproducible: Always

Steps to Reproduce:
1. Create Font object (font1)
2. Call TextUtilities.getTextExtents with font1
3. Dispose font1
4. Create Font object font2 with same settings as font1
5. Call TextUtilities.getTextExtents with font2

Code sample:

	Font font1 = new Font(Display.getDefault(), "Verdana", 8, SWT.NORMAL);
	TextUtilities.INSTANCE.getTextExtents("aaa", font1);
	font1.dispose();
	Font font2 = new Font(Display.getDefault(), "Verdana", 8, SWT.NORMAL);
	TextUtilities.INSTANCE.getTextExtents("aaa", font2);  // will cause exception
Comment 1 Ivan Furnadjiev CLA 2012-04-11 09:01:01 EDT
Actually, this is an RWT problem. Test case:
public void testSetFontOverEqualDisplosedFont() {
  Font font = new Font( display, "font-name", 11, SWT.NORMAL );
  gc.setFont( font );
  font.dispose();

  font = new Font( display, "font-name", 11, SWT.NORMAL );
  gc.setFont( font );

  assertSame( font, gc.getFont() );
}
Comment 2 Ivan Furnadjiev CLA 2012-04-11 16:16:58 EDT
Fixed by moving the check for equal font to ControlGC to skip the GCOperation creation. Same applies to background and foreground colors. JUnit tests added.