Bug 368438

Summary: [JFace] ImageHyperLink.setImage creates a new object everytime it is called
Product: [Eclipse Project] Platform Reporter: Olivier Melois <olivier.melois>
Component: UIAssignee: Tomasz Zarna <tomasz.zarna>
Status: RESOLVED FIXED QA Contact:
Severity: major    
Priority: P3 CC: aljoscha, eclipse.felipe, faure.tristan, markus.kell.r, remy.suen, tomasz.zarna
Version: 3.5.2Keywords: bugday, greatfix, helpwanted
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
See Also: https://git.eclipse.org/r/42238
https://git.eclipse.org/c/platform/eclipse.platform.ui.git/commit/?id=6caa7b47ae99426e6272ac2aeb50c9e42d7a62be
Whiteboard:

Description Olivier Melois CLA 2012-01-12 06:30:48 EST
Build Identifier: 20100218-1602

calling the following method of org.eclipse.ui.forms.widgets.ImageHyperLink : 

public void setImage(Image image) 

creates a disabledImage (a grey version of the original image). This disabled image is not disposed or treated properly. Therefore, using the "setImage" many times triggers an overload of handles. 

Reproducible: Always

Steps to Reproduce:
1.Simply use the ImageHyperLink.setImage method 10000 times ...
Comment 1 Remy Suen CLA 2012-01-12 06:46:07 EST
(In reply to comment #0)
> Build Identifier: 20100218-1602
> 
> calling the following method of org.eclipse.ui.forms.widgets.ImageHyperLink : 
> 
> public void setImage(Image image) 
> 
> creates a disabledImage (a grey version of the original image). This disabled
> image is not disposed or treated properly.

The code seems otherwise.

public void setImage(Image image) {
  this.image = image;
  if (disabledImage != null)
    disabledImage.dispose();
  if (image != null && !image.isDisposed())
    disabledImage = new Image(image.getDevice(), image, SWT.IMAGE_DISABLE);
  if (image == null) {
    disabledImage = null;
  }
}
Comment 2 Olivier Melois CLA 2012-01-12 08:12:36 EST
My problem seem to occur because I have many ImageHyperLinks. Everytime I call setImage, the condition (image != null && !image.isDisposed()) is true, thus a new image is created :

  if (image != null && !image.isDisposed())
    disabledImage = new Image(image.getDevice(), image, SWT.IMAGE_DISABLE);

Is there any way to set an image without creating this new Image ?
Comment 3 Felipe Heidrich CLA 2012-01-12 10:13:22 EST
the code in ImageHyperLink is right IMO.
note that before creating the new disabled image the old one is disabled.
the other place the disabled image needs to be disposed is when the control is disabled.
Comment 4 Tristan Faure CLA 2012-01-12 10:19:54 EST
Hi
you are right the dispose mechanism works correctly.
The problem in our context is that we have several views containing severals image hyperlinks.
For some usage the number of hyperlinks can be huge and the number of system handle too.
I think, just a setter to disable the disabled icon could be enough.
Comment 5 Markus Keller CLA 2012-02-16 14:23:40 EST
> I think, just a setter to disable the disabled icon could be enough.

That wouldn't be a good API. A better solution is to only create the disabledImage lazily in paintHyperlink(..) the first time it is really used (.

Would be great if you could attach a patch for this.
Comment 6 Tomasz Zarna CLA 2015-02-17 09:38:38 EST
I intend to submit the patch within 2 weeks.
Comment 7 Eclipse Genie CLA 2015-02-19 16:31:54 EST
New Gerrit change created: https://git.eclipse.org/r/42238
Comment 9 Wojciech Sudol CLA 2015-03-04 11:58:00 EST
Thanks Tomasz!