Skip to content

Commit 7466be6

Browse files
committed
[Win32] Clean up disposed images when retrieving image list handle #2432
Images inside an ImageList may become disposed. For that reason, methods like indexOf() or add() check for the images inside the list being disposed to null them. This check is missing in the getHandle() method used when requesting the ImageList handle for a different zoom, which can lead to exceptions because of access to a disposed Image instance. This change adapts the ImageList implementation to always consider that an image inside the list may have been disposed. The according cleanup logic is factored out from all places. Fixes #2432
1 parent 1ae0e18 commit 7466be6

File tree

1 file changed

+16
-11
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal

1 file changed

+16
-11
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ImageList.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ public int add (Image image) {
5151
int count = OS.ImageList_GetImageCount (handle);
5252
int index = 0;
5353
while (index < count) {
54-
if (images [index] != null) {
55-
if (images [index].isDisposed ()) images [index] = null;
56-
}
57-
if (images [index] == null) break;
54+
Image imageAtIndex = getOrClearIfDisposed(index);
55+
if (imageAtIndex == null) break;
5856
index++;
5957
}
6058
if (count == 0) {
@@ -71,6 +69,13 @@ public int add (Image image) {
7169
return index;
7270
}
7371

72+
private Image getOrClearIfDisposed(int index) {
73+
if (images[index] != null && images[index].isDisposed()) {
74+
images[index] = null;
75+
}
76+
return images[index];
77+
}
78+
7479
public int addRef() {
7580
return ++refCount;
7681
}
@@ -337,7 +342,7 @@ public long getHandle(int targetZoom) {
337342
long newImageListHandle = OS.ImageList_Create(scaledWidth, scaledHeight, flags, 16, 16);
338343
int count = OS.ImageList_GetImageCount (handle);
339344
for (int i = 0; i < count; i++) {
340-
Image image = images[i];
345+
Image image = getOrClearIfDisposed(i);
341346
if (image != null) {
342347
set(i, image, i, newImageListHandle, targetZoom);
343348
} else {
@@ -372,9 +377,9 @@ public Point getImageSize() {
372377
public int indexOf (Image image) {
373378
int count = OS.ImageList_GetImageCount (handle);
374379
for (int i=0; i<count; i++) {
375-
if (images [i] != null) {
376-
if (images [i].isDisposed ()) images [i] = null;
377-
if (images [i] != null && images [i].equals (image)) return i;
380+
Image potentialMatch = getOrClearIfDisposed(i);
381+
if (potentialMatch != null && potentialMatch.equals(image)) {
382+
return i;
378383
}
379384
}
380385
return -1;
@@ -480,9 +485,9 @@ public int size () {
480485
int result = 0;
481486
int count = OS.ImageList_GetImageCount (handle);
482487
for (int i=0; i<count; i++) {
483-
if (images [i] != null) {
484-
if (images [i].isDisposed ()) images [i] = null;
485-
if (images [i] != null) result++;
488+
Image image = getOrClearIfDisposed(i);
489+
if (image != null) {
490+
result++;
486491
}
487492
}
488493
return result;

0 commit comments

Comments
 (0)