Now that the leak has been discovered, it needs to be fixed. The solution is to dispose()
the Color
once it is finished with, which will be when the view itself is removed.
A quick investigation of ClockWidget
suggests that overriding dispose()
might work. (Note that this is not the correct solution; see later for why.)
Create a
dispose()
method inClockWidget
with the following code:@Override public void dispose() { if(color != null && !color.isDisposed()) color.dispose(); super.dispose(); }
Run the Eclipse application in debug mode (with the tracing enabled, as before) and open and close the view. The output will show something like:
There are 87 Color instances There are 91 Color instances There are 94 Color instances There are 98 Color instances
Remove the
dispose()
method (since it doesn't work as intended) and modify the constructor ofClockWidget
to add an anonymousDisposeListener
that disposes of the associatedColor
object:public ClockWidget...