Bug 570424

Summary: Create JFace/SWT Factory to prevent resource leaks
Product: [Eclipse Project] Platform Reporter: Wim Jongman <wim.jongman>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3    
Version: 4.19   
Target Milestone: ---   
Hardware: PC   
OS: Windows 10   
See Also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=570094
https://git.eclipse.org/r/c/platform/eclipse.platform.ui/+/174947
https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/174948
https://bugs.eclipse.org/bugs/show_bug.cgi?id=561782
Whiteboard:

Description Wim Jongman CLA 2021-01-17 06:02:58 EST
As a follow up to Bug 570094 I found myself creating yet another resource factory to manage "one-off" resource leaks.

JFace provides ResourceManager and *Registry but that API is 

a) intended for long term resource caching.
b) complicated


Instead of 

setTitleImage(new Image(display, inputStream))); // resource leak

Use

setTitleImage(Factory.create(someControl, inputStream)));

// Factory class replacing this specific Image constructor
public static Image create(Control control, InputStream inputStream) {
	Image image = new Image(control.getDisplay(), inputStream);
	control.addDisposeListener(e -> image.dispose());
	return image;
}
Comment 1 Eclipse Genie CLA 2021-01-17 06:55:33 EST
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.ui/+/174947
Comment 2 Andrey Loskutov CLA 2021-01-17 07:00:05 EST
Yep, same needed for Font / Cursor / Region.

May be API should use Supplier<Resource>, not just InputStream, or both. I had lot of code that created some icons / fonts manually but using higher level API, without streams.
Comment 3 Wim Jongman CLA 2021-01-17 09:22:34 EST
(In reply to Andrey Loskutov from comment #2)
> Yep, same needed for Font / Cursor / Region.
> 
> May be API should use Supplier<Resource>, not just InputStream, or both. I
> had lot of code that created some icons / fonts manually but using higher
> level API, without streams.

The inputstream is just an example of one of the constructors.
Comment 4 Eclipse Genie CLA 2021-01-17 10:00:06 EST
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/174948