Bug 199460

Summary: Disposing an image changes its hashCode
Product: [Eclipse Project] Platform Reporter: Michael Valenta <Michael.Valenta>
Component: SWTAssignee: Silenio Quarti <Silenio_Quarti>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: john.arthorne, markus.kell.r, sxenos, tomasz.zarna
Version: 3.3   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Bug Depends on:    
Bug Blocks: 175526, 181215    

Description Michael Valenta CLA 2007-08-09 13:17:10 EDT
When an Image is disposed, the handle of that image is set to 0. This has the side effect that the value returned by the hashCode method changes. This can cause problems if the image is ever used directly or indirectly as a key in a table and the image is disposed before the value stored in the table. One specific case of this involves using a JFace DecorationOverlayIcon in combination with a LocalResourceManager (see bug 175526). Generally, the hashCode of an immutable object like an Image shouldn;t change over the lifespan of the object.

To work around this at the JFace level, I have modified the DecorationOverlayIcon to use an identity hash on the image. This is really only a problem if there are a large number of cases where multiple images share the same handle. My guess is that is rare for images to share a handle. Is this a correct assumption?
Comment 1 Michael Valenta CLA 2007-08-09 13:20:22 EDT
Here's a comment John made on bug 175526:

I think it would be best to fix the implementation of Image equals
and hashCode. Note also that unequal instances of Image become equal to each
other after the images are disposed (since both their handles become zero).
This isn't technically a violation of the equals/hashCode contract, but it's
nevertheless confusing and likely to lead to errors for clients. Otherwise
we'll need to track down and remove any use of Image as hashtable keys.
Comment 2 Stefan Xenos CLA 2007-10-02 23:50:44 EDT
This has caused some troubles for me as well.
Comment 3 John Arthorne CLA 2008-06-10 15:49:42 EDT
For reference, bug 236324 shows another victim of unstable image hash code.
Comment 4 Markus Keller CLA 2010-05-25 06:04:21 EDT
Most subclasses of Resource have the same problem (e.g. Color, Font).

A solution would be to implement isDisposed() using "device == null" and not set the handle to -1 in the implementations of destroy(). That way, the hashCode() stays stable, and the only remaining glitch is that disposed resources from different devices could be considered equal. But that's already the case today, and should rarely be an issue.
Comment 5 Eclipse Webmaster CLA 2019-09-06 16:17:04 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.