August 02, 2005
Disposing resources in SWT
Rumor has it that the SWT uses no garbage collection and forces you to dispose of widgets manually. If you've been avoiding SWT for this reason, know this:
Disposing a widget automatically disposes its children, so closing a window will dispose all contained widgets as well.
There are resources that do not have explicit parents, like fonts and colors. These must be disposed manually if you created them. However, most of the time you will use standard system colors and fonts which you don't have to dispose because they are managed by SWT.
When you still need to create a custom resource (like an icon image), SWT has a class
JFaceResourceswhich offers a central registry for images, fonts and colors. This way you can reuse resources and don't need to worry about their later disposal.
One last piece of advice for SWT developers: If you absolutely want to dispose a resource manually (e.g. dispose a large
image when its containing composite is disposed), do not override dispose() of the containing widget.
The dispose() method will not be called if the containing parent is disposed. Instead add a DisposeListener and clean up there.
Comments
Post a comment
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)