Whoa - Back to malloc/free?
Here's an interesting paper on SWT usage:
When programming in a GUI operating system, you allocate operating system resources for widgets, images, fonts, and other graphical objects. Since there is a platform limit on the amount of resources you can allocate, you must be careful to free any objects that you allocate in your application. If you allocate a resource and do not free it when you are done with it, your application is "leaking" resources. An application that repeatedly leaks resources will eventually consume all of the available resources, forcing the user to reboot the operating system. Fortunately, SWT makes resource allocation and disposal a straightforward process. There are only two rules that you need to remember when allocating and freeing SWT resource-based objects: Rule 1: If you created it, you dispose it. Rule 2: Disposing the parent disposes the childrenOk, that seems like a huge step backwards to me. It gets more interesting:
This is a simple rule. SWT makes it easy for you to remember when operating system resources are allocated; all SWT resource-based objects (like Color, Cursor, Display, Font, GC, Image, Printer, Region, Widget and subclasses) allocate any needed operating system resources in their constructor. There are no exceptions to this rule. There are no methods in SWT (other than constructors) that allocate operating system resources that the programmer must manage. If you didn't call the constructor, then you don't need to free the resources, so don't call dispose on the object. For example, in the following line of code, an operating system font is allocated:I suppose developers would internalize (most) of that - but that introduces some nice opportunities for memory leaks and - whatever happens if you call dispose() inappropriately. This is progress? Interestingly enough, this isn't just me as a Smalltalker being smug. I found this here, on a Java developer's blog. Some of his comments:Font font = new Font (display, "Courier", 10, SWT.NORMAL);Since you called the Font constructor to create the resource, you must dispose the font when you are finished with it, as follows:font.dispose();In the following line of code, however, a constructor is not called:Font font = control.getFont ();Therefore, you must not call dispose. The font variable does contain an operating system font resource, but you did not allocate it. If you were to dispose of this font, you would be leaving the control without a font! The results are undefined. So, if you are using any getter that returns an SWT resource-based object that you did not allocate, do not dispose the object.
cbeust on SWT and memory management:Back to future gang! Looks like the memory leak business will be alive and well in Java land....However, I have to agree with Danno about resource handling: the fact that SWT forces you to free manually the resources you allocate is a huge step backward that is going to become a big liability for this toolkit in the near future.

