eclipse.platform.swt icon indicating copy to clipboard operation
eclipse.platform.swt copied to clipboard

Avoid overriding finalize()

Open bt90 opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe.

The current implementation to make Resource.setNonDisposehandler(Consumer) work, uses finalize() under the hood:

https://github.com/eclipse-platform/eclipse.platform.swt/blob/4d6832927eb8874dccb0c59eac5a761ce9ea025f/bundles/org.eclipse.swt/Eclipse%20SWT/common/org/eclipse/swt/graphics/Resource.java#L74-L81

This method is deprecated for removal since Java 18 as described in JEP 421.

Describe the solution you'd like

We should rewrite this functionality using PhantomReferences and a daemon thread polling from the shared ReferenceQueue.

Describe alternatives you've considered

The cleaner API might also work:

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ref/Cleaner.html

bt90 avatar Sep 12 '24 07:09 bt90

I already made some PO to use Cleaner API instead:

  • https://github.com/eclipse-platform/eclipse.platform.swt/pull/1054

this has the advantage that one still can use dispose() to clear early, wait for the GC (and probably log), support clearing of Weak/SoftReference and so on.

laeubi avatar Sep 12 '24 07:09 laeubi

here is a working commit that was rejected: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/185578/24/bundles/org.eclipse.swt/Eclipse+SWT/common/org/eclipse/swt/graphics/Resource.java#b74

jukzi avatar Sep 12 '24 12:09 jukzi

Is there any reason why we can't let the Cleaner do the disposal, effectively making dispose() a best practice instead of a necessity?

bt90 avatar Sep 12 '24 13:09 bt90

Aa cleaner depends on GC, which has literally no guaranties to run. Resources are OS objects which need to be released otherwise the OS runs in out of resource; and- a cleaner does not run in swt thread.

jukzi avatar Sep 12 '24 13:09 jukzi

My proposed solution: #1702

otbutz avatar Jan 02 '25 14:01 otbutz