Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 342943 - Control#getFont returns disposed font instead of font used by the control
Summary: Control#getFont returns disposed font instead of font used by the control
Status: RESOLVED INVALID
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 321135
  Show dependency tree
 
Reported: 2011-04-15 06:15 EDT by Ivan Furnadjiev CLA
Modified: 2011-04-15 14:01 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.