Index: trunk/src/org/openstreetmap/josm/gui/dialogs/DialogsPanel.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/DialogsPanel.java	(revision 18685)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/DialogsPanel.java	(revision 18686)
@@ -105,4 +105,30 @@
         } else {
             dlg.hideDialog();
+        }
+    }
+
+    /**
+     * Remove a {@link ToggleDialog} from the list of known dialogs and trigger reconstruct.
+     * @param toggleDialog The dialog to remove
+     * @since 18686
+     */
+    public void remove(ToggleDialog toggleDialog) {
+        remove(toggleDialog, true);
+    }
+
+    /**
+     * Remove a {@link ToggleDialog} from the list of known dialogs.
+     * @param toggleDialog The dialog to remove
+     * @param doReconstruct <code>true</code> if reconstruction should be triggered.
+     * @since 18686
+     */
+    public void remove(ToggleDialog toggleDialog, boolean doReconstruct) {
+        toggleDialog.setDialogsPanel(null);
+        final JPanel oldPanel = panels.get(allDialogs.indexOf(toggleDialog));
+        allDialogs.remove(toggleDialog);
+        panels.remove(oldPanel);
+        mSpltPane.remove(oldPanel);
+        if (doReconstruct && !allDialogs.isEmpty()) {
+            reconstruct(Action.ELEMENT_SHRINKS, toggleDialog);
         }
     }
@@ -326,5 +352,5 @@
     @Override
     public void destroy() {
-        for (ToggleDialog t : allDialogs) {
+        for (ToggleDialog t : new ArrayList<>(allDialogs)) {
             try {
                 t.destroy();
Index: trunk/src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java	(revision 18685)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java	(revision 18686)
@@ -478,5 +478,8 @@
     @Override
     public void destroy() {
-        dialogsPanel = null;
+        if (dialogsPanel != null) {
+            dialogsPanel.remove(this);
+            dialogsPanel = null;
+        }
         rememberHeight();
         closeDetachedDialog();
Index: trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageViewerDialog.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageViewerDialog.java	(revision 18685)
+++ trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageViewerDialog.java	(revision 18686)
@@ -145,4 +145,10 @@
      */
     private static void destroyInstance() {
+        MapFrame map = MainApplication.getMap();
+        synchronized (ImageViewerDialog.class) {
+            if (dialog != null && map != null && map.getToggleDialog(ImageViewerDialog.class) != null) {
+                map.removeToggleDialog(dialog);
+            }
+        }
         dialog = null;
     }
Index: trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/ImageViewerDialogTest.java
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/ImageViewerDialogTest.java	(revision 18685)
+++ trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/ImageViewerDialogTest.java	(revision 18686)
@@ -21,4 +21,5 @@
 import javax.swing.JPanel;
 
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.extension.RegisterExtension;
@@ -44,8 +45,14 @@
 
     private ImageViewerDialog dialog;
+
     @BeforeEach
     void setup() {
         this.dialog = ImageViewerDialog.getInstance();
         this.dialog.displayImages(null);
+    }
+
+    @AfterEach
+    void tearDown() {
+        this.dialog.destroy();
     }
 
Index: trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/ImagesLoaderTest.java
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/ImagesLoaderTest.java	(revision 18685)
+++ trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/ImagesLoaderTest.java	(revision 18686)
@@ -10,25 +10,24 @@
 import java.util.List;
 
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
 import org.openstreetmap.josm.TestUtils;
 import org.openstreetmap.josm.gui.MainApplication;
 import org.openstreetmap.josm.gui.layer.GpxLayer;
 import org.openstreetmap.josm.io.GpxReader;
-import org.openstreetmap.josm.testutils.JOSMTestRules;
-
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
 
 /**
  * Unit tests of {@link ImagesLoader} class.
  */
+@BasicPreferences
 class ImagesLoaderTest {
 
-    /**
-     * We need prefs for this.
-     */
-    @RegisterExtension
-    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
-    public JOSMTestRules test = new JOSMTestRules().preferences();
+    @AfterEach
+    void tearDown() {
+        if (ImageViewerDialog.hasInstance()) {
+            ImageViewerDialog.getInstance().destroy();
+        }
+    }
 
     /**
Index: trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/WikimediaCommonsLoaderTest.java
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/WikimediaCommonsLoaderTest.java	(revision 18685)
+++ trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/WikimediaCommonsLoaderTest.java	(revision 18686)
@@ -11,4 +11,5 @@
 import java.util.List;
 
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
@@ -29,4 +30,11 @@
     static void beforeAll() {
         HttpClient.setFactory(Http1Client::new);
+    }
+
+    @AfterEach
+    void tearDown() {
+        if (ImageViewerDialog.hasInstance()) {
+            ImageViewerDialog.getInstance().destroy();
+        }
     }
 
