Skip to content

Commit

Permalink
Fix Colocalisation_Test window crash on multiple runs
Browse files Browse the repository at this point in the history
If a script uses Colocalisation_Test, and closes the result window,
subsequent attempts to run Colocalisation_Test will crash because the
reference to the window will still be there, but the attempts to write
to the window will crash because the window was closed. To verify,
revert changes to Colocalisation_Test.java & run the new test case added
- the 2nd attempt to run coloc will fail.

The solution was to cleanup the window ref when a window is closed.

Signed-off-by: Curtis Rueden <[email protected]>
  • Loading branch information
pvlaraia authored and ctrueden committed Aug 27, 2024
1 parent 1c5f75e commit 087405d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/main/java/sc/fiji/coloc/Colocalisation_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import ij.text.TextWindow;

import java.awt.Rectangle;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.DecimalFormat;

public class Colocalisation_Test implements PlugIn
Expand Down Expand Up @@ -701,10 +703,29 @@ public void correlate(ImagePlus imp1, ImagePlus imp2, ImagePlus imp3)
"\t" +df0.format(iterations)+
"\t" + randMethod+
"\t" +strPSF;
if (textWindow == null)
if (textWindow == null) {
textWindow = new TextWindow("Results",
Headings2, str, 400, 250);
else {
Headings2, str, 400, 250);
textWindow.addWindowListener(new WindowListener() {
@Override
public void windowClosed(WindowEvent arg0) {
textWindow = null;
}

@Override
public void windowActivated(WindowEvent arg0) {}
@Override
public void windowClosing(WindowEvent arg0) {}
@Override
public void windowDeactivated(WindowEvent arg0) {}
@Override
public void windowDeiconified(WindowEvent arg0) {}
@Override
public void windowIconified(WindowEvent arg0) {}
@Override
public void windowOpened(WindowEvent arg0) {}
});
} else {
textWindow.getTextPanel().setColumnHeadings(Headings2);
textWindow.getTextPanel().appendLine(str);
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/sc/fiji/coloc/tests/ColocalisationGUITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package sc.fiji.coloc.tests;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.awt.Window;
import java.awt.event.WindowEvent;

import org.junit.Test;

import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.plugin.ChannelSplitter;
import sc.fiji.coloc.Colocalisation_Test;

public class ColocalisationGUITest extends ColocalisationTest {
@Test
public void testThatWindowRefIsCleanedUpIfClosed() {
ImagePlus img = IJ.openImage("http://imagej.net/images/FluorescentCells.zip");
ImagePlus[] splitImg = ChannelSplitter.split(img);
Colocalisation_Test colocalisationTest = new Colocalisation_Test();

// run module once, it'll open results textWindow
colocalisationTest.correlate(splitImg[0], splitImg[1], splitImg[2]);

// check textWindow is open, and close it
Window resultsWindow = WindowManager.getWindow("Results");
assertTrue(resultsWindow.isVisible());
resultsWindow.dispatchEvent(new WindowEvent(resultsWindow, WindowEvent.WINDOW_CLOSING));
assertFalse(resultsWindow.isVisible());

// run module again
colocalisationTest.correlate(splitImg[0], splitImg[1], splitImg[2]);

// check textWindow is open, .correlate didnt crash!
resultsWindow = WindowManager.getWindow("Results");
assertTrue(resultsWindow.isVisible());

}
}

0 comments on commit 087405d

Please sign in to comment.