Adventures in Java: 1

Duke, the Java Mascot, in the waving pose. Duk...

Image via Wikipedia

I am working in Java, using Eclipse as both IDE and platform. It’s fun and confuses people when I can run the thing i am working on in a separate editor in the same window of eclipse while editing the code in another editor tab in the same window. Confused yet?

Anyway not what I was writing about. I’m targeting Eclipse and using GEF (the Graphical Editing Framework) Draw2D and SWT. SWT is where the problems begin. SWT uses widgets and items that work with the underlying OS and Window Manager so that the look and feel is much like the rest of the programs on the target platform. So the handles and memory of the widgets being used are owned by the OS and so Java doesn’t automatically garbage collect them.

So you have to and it’s not always obvious. And you have to be careful that you don’t garbage collect them and then use them again or that you don’t garbage collect the item if you didn’t create it. It sounds simple and it is mostly. SO if you do something like

Color mycolor = new Color(null, 255,50,50);

then be sure somewhere after you use it you have

if(!mycolor.isDisposed()) mycolor.dispose();

And if it’s someplace that you might reassign the mycolor with a new value then dispose the old before assigning a new one or you can change them instead of making a new one.

Also when working with Java and SWT in eclipse there is sleak, which is a tool for checking swt leaks. (Download at eclipse.org)

Advertisements

About echlinm

Computer Programmer/Systems Analyst/Hacker S31
This entry was posted in Computers and Internet, Fun, Open Source, Programming and tagged , , , . Bookmark the permalink.

2 Responses to Adventures in Java: 1

  1. ignorante says:

    I’m a big fan of Swing. I worked with SWT, too.

  2. Pingback: Adventures in Java: Java Eclipse debug and Booleans | Borg or No (S31)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s