Skip to content

Commit 07c8e20

Browse files
Fix Resource leak tracking issue on Windows
On Windows, SWT resources supporting multiple zoom levels are registered via Device.registerResourceWithZoomSupport(), which retains strong references to these resources. This prevents garbage collection and breaks leak detection using Cleaner. Switching to use WeakReference to allow proper GC behavior and enable the resource tracker to report non-disposed resources correctly. Fixes: #2335
1 parent 3bfde61 commit 07c8e20

File tree

1 file changed

+15
-4
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+15
-4
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.swt.graphics;
1515

1616

17+
import java.lang.ref.*;
1718
import java.util.*;
1819
import java.util.concurrent.*;
1920

@@ -61,7 +62,7 @@ public abstract class Device implements Drawable {
6162
String[] loadedFonts;
6263

6364
volatile boolean disposed;
64-
private Set<Resource> resourcesWithZoomSupport = ConcurrentHashMap.newKeySet();
65+
private Set<WeakReference<Resource>> resourcesWithZoomSupport = ConcurrentHashMap.newKeySet();
6566

6667
/*
6768
* TEMPORARY CODE. When a graphics object is
@@ -964,11 +965,11 @@ protected int getDeviceZoom () {
964965
}
965966

966967
void registerResourceWithZoomSupport(Resource resource) {
967-
resourcesWithZoomSupport.add(resource);
968+
resourcesWithZoomSupport.add(new WeakReference<>(resource));
968969
}
969970

970971
void deregisterResourceWithZoomSupport(Resource resource) {
971-
resourcesWithZoomSupport.remove(resource);
972+
resourcesWithZoomSupport.remove(new WeakReference<>(resource));
972973
}
973974

974975
/**
@@ -982,7 +983,17 @@ public static void win32_destroyUnusedHandles(Display display) {
982983
for (Monitor monitor : display.getMonitors()) {
983984
availableZoomLevels.add(DPIUtil.getZoomForAutoscaleProperty(monitor.getZoom()));
984985
}
985-
for (Resource resource: ((Device) display).resourcesWithZoomSupport) {
986+
Set<WeakReference<Resource>> resources = ((Device) display).resourcesWithZoomSupport;
987+
Iterator<WeakReference<Resource>> iterator = resources.iterator();
988+
989+
while (iterator.hasNext()) {
990+
WeakReference<Resource> ref = iterator.next();
991+
Resource resource = ref.get();
992+
if (resource == null) {
993+
// Clean up dead reference
994+
iterator.remove();
995+
continue;
996+
}
986997
resource.destroyHandlesExcept(availableZoomLevels);
987998
}
988999
}

0 commit comments

Comments
 (0)