About disposing ressources [message #1027103] |
Tue, 26 March 2013 14:56 |
hortiz Mising name Messages: 96 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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03203 seconds