Bug 80324 - Add dispose listeners to Images, Fonts, and Colors
Summary: Add dispose listeners to Images, Fonts, and Colors
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Steve Northover CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-12-06 16:49 EST by Stefan Xenos CLA
Modified: 2015-04-21 01:59 EDT (History)
1 user (show)

See Also:


Attachments
Patch for org.eclipse.swt (10.72 KB, patch)
2005-06-17 19:10 EDT, Stefan Xenos CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stefan Xenos CLA 2004-12-06 16:49:13 EST
Currently, it is quite hard to debug "graphic is disposed" errors. The exception
is thrown long after the image is disposed, making it hard to track down the
buggy code that disposed the image.

If it were possible to attach dispose listeners to Images, Fonts and Colors,
then the owner of the object to attach a listener which throws an exception if
some other code attempts to dispose its Image. This would generate a stack trace
that includes the buggy code that disposed the image.
Comment 1 Steve Northover CLA 2005-04-04 15:20:48 EDT
I'm sorry but we didn't get to this for 3.1.

My current thinking would be to add this as part of a tracing API.  The 
problem with adding callbacks to the objects themselves is that there can be 
two different graphics objects that have the same handle (ie. Control.getFont
() can return a new instance) and both would need to have the callback.
Comment 2 Stefan Xenos CLA 2005-04-04 15:57:12 EDT
The important part of this PR is that it would be possible to catch disposal
errors when the graphic is disposed rather than the next time it is used. It
would be possible to solve this without dispose listeners. Here's how:

ALTERNATIVE 1: Reference counting

Add incRef() and decRef() methods to Image, Font, Color, etc. Nothing special
will happen when the refcount reaches zero (clients still need to dispose their
graphics as normal), but if an attempt is made to dispose the graphic while the
refcount is nonzero, it will immediately throw an SWTException.

This would solve my primary use case, and it wouldn't matter if SWT shares
handles behind the scenes. I'm not relying on the refcount to perform any
cleanup -- I'm just using it to ensure that someone I hand an Image to won't try
to dispose that Image.


ALTAERNATIVE 2: Locking/unlocking

It would also be possible to solve this problem by adding the following methods:

void lock(Object key);
void unlock(Object key);

If an object is locked, any attempt to re-lock it, dispose it, or unlock it with
a non-identical key will throw an exception.

Comment 3 Steve Northover CLA 2005-04-04 16:37:13 EDT
Neither will work because instances of graphics objects aren't unique ... but 
that doesn't matter for the problem you are trying to solve.
Comment 4 Stefan Xenos CLA 2005-06-17 19:10:04 EDT
Created attachment 23501 [details]
Patch for org.eclipse.swt

Adds a locking/unlocking scheme with integer keys and minimal overhead.
Comment 5 Stefan Xenos CLA 2007-07-05 10:43:15 EDT
Steve: what if we had a global service I could listen to (sortof like Display filters) in order to monitor the disposal of graphics resources. 

Something like:

Display.getCurrent().addGraphicDisposalListener(graphicObject, listener);

Whenever a graphic resource is disposed, it could notify the listeners via the global service. It wouldn't rely on the resources being unique - disposing any copy of the resource would send the notification.