It is necessary to know how many resources are allocated in order to know whether the leak has been plugged or not. Fortunately, SWT provides a mechanism to do this via the Display
and the DeviceData
class. Normally, this is done by a separate plug-in, but in this example, the ClockView
will be modified to show this behavior.
At the start of the
ClockView
class'screatePartControl
method, add a call to obtain the number of allocated objects, via theDeviceData
of theDisplay
class:public void createPartControl(Composite parent) { Object[] objects = parent.getDisplay().getDeviceData().objects;
Iterate through the allocated
objects
, counting how many are instances ofColor
:int count = 0; for (int i = 0; i < objects.length; i++) { if (objects[i] instanceof Color) { count++; } }
Print the
count
to the standard error stream:System.err.println("There are " + count + " Color instances");
Now run the code in debug mode and show the Clock View...