From a9d64a85c8b92f306f8c76a8c570522781d05f05 Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Wed, 30 Oct 2024 15:18:17 +0100 Subject: [PATCH] De-register resources with zoom support from device Resources with zoom support are registered at their device so that handles for zoom levels that are not necessary anymore can be disposed. When these resources are destroyed, they are currently not de-registered form their device, potentially leading to resource leaks. This change makes the resources de-register themselves form their device when being destroyed. Supplements https://github.com/eclipse-platform/eclipse.platform.swt/pull/1477 --- .../Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java | 4 ++++ .../Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java | 1 + .../Eclipse SWT/win32/org/eclipse/swt/graphics/Path.java | 1 + .../Eclipse SWT/win32/org/eclipse/swt/graphics/Pattern.java | 1 + .../Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java | 1 + .../Eclipse SWT/win32/org/eclipse/swt/graphics/Transform.java | 1 + 6 files changed, 9 insertions(+) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java index e136a87d87b..994b3eaeb3d 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java @@ -959,6 +959,10 @@ void registerResourceWithZoomSupport(Resource resource) { resourcesWithZoomSupport.add(resource); } +void deregisterResourceWithZoomSupport(Resource resource) { + resourcesWithZoomSupport.remove(resource); +} + /** * Destroys the handles of all the resources in the resource tracker by * identifying the zoom levels which is not valid for any monitor diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java index 7d57b456a2c..3a8cbb748df 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java @@ -1187,6 +1187,7 @@ long initNative(String filename, int zoom) { @Override void destroy () { + device.deregisterResourceWithZoomSupport(this); if (memGC != null) memGC.dispose(); destroyHandle(); memGC = null; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Path.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Path.java index 7949d08a685..bcd482b549c 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Path.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Path.java @@ -437,6 +437,7 @@ void cubicToInPixels(float cx1, float cy1, float cx2, float cy2, float x, float @Override void destroy() { + device.deregisterResourceWithZoomSupport(this); zoomLevelToHandle.values().forEach(Gdip::GraphicsPath_delete); zoomLevelToHandle.clear(); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Pattern.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Pattern.java index 36e9d97bef8..658eb83c6fc 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Pattern.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Pattern.java @@ -261,6 +261,7 @@ void setImageHandle(Image image, int zoom) { @Override void destroy() { + device.deregisterResourceWithZoomSupport(this); for (long handle: zoomLevelToHandle.values()) { destroyHandle(handle); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java index 5219f385e52..da5cff40c3f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java @@ -223,6 +223,7 @@ public boolean contains (Point pt) { @Override void destroy () { + device.deregisterResourceWithZoomSupport(this); zoomToHandle.values().forEach(handle -> OS.DeleteObject(handle)); zoomToHandle.clear(); operations.clear(); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Transform.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Transform.java index c7174509209..38d3f3737a1 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Transform.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Transform.java @@ -156,6 +156,7 @@ static float[] checkTransform(float[] elements) { @Override void destroy() { + device.deregisterResourceWithZoomSupport(this); zoomLevelToHandle.values().forEach(Gdip::Matrix_delete); zoomLevelToHandle.clear(); }