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

Bug 342943

Summary: Control#getFont returns disposed font instead of font used by the control
Product: [Eclipse Project] Platform Reporter: Ivan Furnadjiev <ivan>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: RESOLVED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: eclipse.felipe, ruediger.herrmann
Version: 3.7   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Bug Depends on:    
Bug Blocks: 321135    

Description Ivan Furnadjiev CLA 2011-04-15 06:15:42 EDT
When you dispose the font set on the Control (Label for example) the Control continue to function without exception with some default system font. All sequential operations like layout, pack, computeSize are based on this default font. But Control#getFont() still returns the old disposed font. I think that Control#getFont() should return the font which is currently used by the Control and not the disposed one. Snippet:
---
Label label = new Label( parent, SWT.NONE );
Font font = new Font( parent.getDisplay(), "Arial", 20, SWT.BOLD );
label.setText( "Test label" );
label.setFont( font );
font.dispose();
label.redraw();
parent.layout();
Font labelFont = label.getFont();
---
Comment 1 Felipe Heidrich CLA 2011-04-15 09:57:01 EDT
You can not dispose the font that is still being used, some systems the application can crash if yo do that.
Comment 2 RĂ¼diger Herrmann CLA 2011-04-15 10:51:19 EDT
(In reply to comment #1)
> You can not dispose the font that is still being used, some systems the
> application can crash if yo do that.
This is good news for RWT as it cannot work with disposed fonts either :)
The use case described in bug 321135 is: when to dispose of a font (or any other resource) that is used for a specific widget and should be freed 'together' with the widget.
Would it be legal to dispose of the font in the widgets dispose event?
Comment 3 Felipe Heidrich CLA 2011-04-15 14:01:47 EDT
(In reply to comment #2)
> (In reply to comment #1)
> > You can not dispose the font that is still being used, some systems the
> > application can crash if yo do that.
> This is good news for RWT as it cannot work with disposed fonts either :)
> The use case described in bug 321135 is: when to dispose of a font (or any
> other resource) that is used for a specific widget and should be freed
> 'together' with the widget.
> Would it be legal to dispose of the font in the widgets dispose event?

Usually application store all the resouces they create and release them at the end (after the main event loop returns). See ControlExample for a example.
But the answer is yes to your question, you can also use the dispose event of the Display, or just
widget.dispose();
font.dispose(); // -> no one else references this font, you created the font explicitely.