Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » About disposing ressources(Can I dispose a font once it has been set into a widget ?)
About disposing ressources [message #1027103] Tue, 26 March 2013 14:56 Go to next message
hortiz Mising name is currently offline hortiz Mising nameFriend
Messages: 85
Registered: July 2009
Member
Hi,

I read the document "The Standard Widget Toolkit PART 2: Managing Operating System Resources" (http://www.eclipse.org/articles/swt-design-2/swt-design-2.html) and I have a question about this sentence:

"Since you called the Font constructor to create the resource, you must dispose the font when you are finished with it"

What does "having finished with it" mean exactly ?
If the widget for which I have created a new Font is still visible, can I dispose the font anyway ? Till now, I always believed that I can dispose the font or the color linked to a control only when the latter gets disposed.

I wrote a little snippet and I was very surprised to see that the label's background and font are ok even if they have been disposed:

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class DisposeTest {

	public static void main(String[] args) {

		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new RowLayout());

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new RowLayout());

		Label label = new Label(composite, SWT.NONE);
		Color color = new Color(display, 255, 0, 0);
		label.setBackground(color);
		label.setText("Hello World");
		Font font = new Font (display, "Courier", 10, SWT.NORMAL);
		label.setFont(font);
		
		font.dispose();
		color.dispose();
		
		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep ();
			}
		}

		display.dispose();
	}
}



Can someone explain this ?
Thank you

Re: About disposing ressources [message #1027109 is a reply to message #1027103] Tue, 26 March 2013 15:07 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 2013-03-26 15:56, hortiz Mising name wrote:
> I read the document "The Standard Widget Toolkit PART 2: Managing
> Operating System Resources"
> (http://www.eclipse.org/articles/swt-design-2/swt-design-2.html) and I
> have a question about this sentence:
>
> "Since you called the Font constructor to create the resource, you must
> dispose the font when you are finished with it"
>
> What does "having finished with it" mean exactly ?

It means that the corresponding control does no longer use the font,
e.g. when the control is disposed or if you have assigned a different
font to it.

> If the widget for which I have created a new Font is still visible, can
> I dispose the font anyway ? Till now, I always believed that I can
> dispose the font or the color linked to a control only when the latter
> gets disposed.

This is the correct assumption, anything else relies on non-portable
system-specific behaviour.

> I wrote a little snippet and I was very surprised to see that the
> label's background and font are ok even if they have been disposed:

I'm pretty sure that there is no guarantee that this works correctly. It
may work on one system or the other but you should not rely on that.

HTH & Greetings from Bremen,

Daniel Krügler
Re: About disposing ressources [message #1027307 is a reply to message #1027109] Tue, 26 March 2013 20:55 Go to previous message
hortiz Mising name is currently offline hortiz Mising nameFriend
Messages: 85
Registered: July 2009
Member
Hi Daniel,

Thanks a lot, indeed it helps me a lot !
I got very confused with this snippet but I'm reassured now with your answer.

Greetings from Grenoble,
Helene ORTIZ
Previous Topic:OLE Outlook
Next Topic:Global Key Listener
Goto Forum:
  


Current Time: Fri Dec 19 03:21:47 GMT 2014

Powered by FUDForum. Page generated in 0.01569 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software