-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[gtk] Make Fonts GC disposable #1054
base: master
Are you sure you want to change the base?
[gtk] Make Fonts GC disposable #1054
Conversation
69201cf
to
1c5de02
Compare
1c5de02
to
2b249d4
Compare
I adjusted the title a bit now to make clear I don't want to replace the current disposal of resources, but make them GC disposable (that is for example it would be possible to hold Weak/Soft/PhantomReferences of items). |
That seems very good and can probably prevent some "no more handles" error from happening. |
Yes this basically could (and should) be applied everywhere we are holding a native resource that must be disposed when the object is disposed. (Colors don't need to be disposed anyways but thats a different topic). |
One example where it is useful to only hold a Currently with SWT I only have two choices:
A common usage for this would be some kind of a custom drawn control, where I want to store the last drawn state as an Image and repaint just paint the image. |
Currently handling fonts in SWT is hard and cumbersome, because one can't use the usual java techniques users are forced to either explicitly manage fonts (what maybe retain them longer as needed) or even create and dispose them for individual widgets (what hurts performance). This also leads to a lot of boilerplate code e.g. installing dispose handlers or having managed caches or local resource managers as well as requires documentation who 'owns' a font for example set on a label. Especially for newcommers or seldom users of SWT this is hard to get right and hinders them to use SWT correctly as java is usually managing objects automatically. This now introduces the concept of Cleaners added to Java 9+ that allows automatic management of external resources that get automatically disposed when the JVM decides that an object is only phantom reachable and eligable for garbage collection.
2b249d4
to
f534d57
Compare
Current State
Currently handling fonts in SWT is hard and cumbersome, because one can't use the usual java techniques users are forced to either explicitly manage fonts (what maybe retain them longer as needed) or even create and dispose them for individual widgets (what hurts performance). This also leads to a lot of boilerplate code e.g. installing dispose handlers or having managed caches or local resource managers as well as requires documentation who 'owns' a font for example set on a label. Especially for newcommers or seldom users of SWT this is hard to get right and hinders them to use SWT correctly as java is usually managing objects automatically.
What this changes
This now introduces the concept of Cleaners added to Java 9+ that allows automatic management of external resources that get automatically disposed when the JVM decides that an object is only phantom reachable and eligible for garbage collection.
I also used this to have a
Handle
act as a proxy to the native method calls so these are much more readable and the grouped together even related calls or required transformations to reduce code duplication.Further considerations
This itself is just meant as an initial starting point, we actually should extends this concept to any native resource handled by SWT (e.g. Images) so it become much more convenient for users to use these. I just choosing a font here as the only managed resource is a long pointer.
I also like to encourage users of other platforms (Win/Mac) to use this as a hint to prototype something similar like this as well so we can evaluate how it works out.
Also this will not replace
dispose()
or make it completely redundant as of course a user can always better dispose a resource directly if it is known that it is no longer in use. Also it might not be suitable for any "root objects" like aDisplay
that is like aThread
in java some kind of UI root that always needs to be disposed explicitly.