Bug 570424 - Create JFace/SWT Factory to prevent resource leaks
Summary: Create JFace/SWT Factory to prevent resource leaks
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.19   Edit
Hardware: PC Windows 10
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-01-17 06:02 EST by Wim Jongman CLA
Modified: 2021-01-23 06:51 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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