Bug 561782 - SWT Resources should dispose itself to avoid native handle leaks
Summary: SWT Resources should dispose itself to avoid native handle leaks
Status: RESOLVED WONTFIX
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.16   Edit
Hardware: PC Windows 10
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-04-05 06:36 EDT by Conrad Groth CLA
Modified: 2021-01-23 06:51 EST (History)
7 users (show)

See Also:


Attachments
font leak (708.66 KB, image/gif)
2020-04-05 06:36 EDT, Conrad Groth CLA
no flags Details
Font disposes itself automatically (788.09 KB, image/gif)
2020-04-05 06:38 EDT, Conrad Groth CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Conrad Groth CLA 2020-04-05 06:36:44 EDT
Created attachment 282343 [details]
font leak

In a huge RCP product I recently did a SWT leak anylsis with SLeak on Windows and I found the following situation quite often:
`FontData font = display.getSystemFont().getFontData()[0];`
`control.setFont(new Font(display, font.getName(), font.getHeight(), SWT.BOLD));`

Without disposing the Font we have a leak of OS graphic handles. I think that many Java developers are so used to the comfort of garbage collection, that they easily forget about the special care they have to take with SWT resources. And why shouldn't SWT also rely on the garbage collection and make lifes of thousands of developers easier?

As long as you don't store the Font in a variable and the Control you've assigned the Font to is diposed, nobody holds a reference to that Font anymore and it's eligable for garbage collection. I simulated that situation with assigning new Fonts to a Control again and again. Because the Control stored only the last assigned Font, the other Fonts are garbage collected. See the attached animated GIF to see how the number of Font objects in Yourkit corresponds to the number of GDI objects, but after GC there are only two Font objects while the GDI objects are still there.

For the second animated GIF I just added the following method to the org.eclipse.swt.graphics.Resource class: 
`@Override`
`protected void finalize() {`
`	dispose();`
`}`

You can now see that the GDI objects are also freed.
Comment 1 Conrad Groth CLA 2020-04-05 06:38:06 EDT
Created attachment 282344 [details]
Font disposes itself automatically
Comment 2 Andrey Loskutov CLA 2020-04-05 07:41:24 EDT
(In reply to Conrad Groth from comment #0)
> For the second animated GIF I just added the following method to the
> org.eclipse.swt.graphics.Resource class: 
> `@Override`
> `protected void finalize() {`
> `	dispose();`
> `}`
> 
> You can now see that the GDI objects are also freed.

I only wonder why the creators of SWT haven't considered this as viable solution (and this is a basic technique for such resources). Have you tried to search in the SWT git history? Also may be someone is still hanging on platform mailing list, I would ask there. 

I believe we might have resources not directly connected to other objects, often it is new Something(display); calls that just create native widgets and don’t care about the object reference.

May be Fonts are special, not sure. 

In any case, great care is needed here, because disposing native resources that shouldn't be disposed may lead to JVM crashes, not necessarily immediately.
Comment 3 Lakshmi P Shanmugam CLA 2020-04-06 00:50:21 EDT
Please see this SWT article on Managing Operating System Resources - https://www.eclipse.org/articles/swt-design-2/swt-design-2.html
Comment 4 Lakshmi P Shanmugam CLA 2020-04-06 00:52:41 EDT
(In reply to Lakshmi Shanmugam from comment #3)
> Please see this SWT article on Managing Operating System Resources -
> https://www.eclipse.org/articles/swt-design-2/swt-design-2.html

In addition to the reasons specified in the article, we can't change the behavior as the existing client code already creates a resource and disposes it. If SWT starts disposing the resources itself it'll break all clients.
Comment 5 Lars Vogel CLA 2020-04-06 06:17:59 EDT
Adding Nikita to cc

IIRC Nikita mentioned in the past, that the disposal might not be necessary anymore for certain resources if some API is adjusted.
Comment 6 Thomas Schindl CLA 2020-04-06 06:31:30 EDT
I can only provide the input that - Qtjambi tried to clean things up with the help of GC and then ran into many many edge cases. Some of them could be fixed generic, others required developers to take special action.
Comment 7 Wim Jongman CLA 2021-01-23 06:51:24 EST
Hi Conrad,

I'm not sure if you have followed the recent developments but this bug has awoken the universe. 

See bug 569752 and bug 570424