Hello, folks,
This is my first topic at this forum, so please be merciful. I wrote a plugin using Jython and it happily worked on old ImageJ versions, but started to crash with newer versions. In the plugin, I select points in an image and save them as ROIs in a zip file. This utilizes a MouseListener class, so I use global variables iROI and xlist to keep the track of multiple ROIs. The plugin behaves normally for 4-7 clicks, and then crashes with a message global name 'updateROIs' is not defined.
This happens with some variability in the number of clicks. It seems that the system destroys my function updateROIs() while the program is running. Sometimes it crashes with error messages about global variables as well, like iROI.
Does anyone know what’s happening behind the curtains here? Or should I just re-write my code in Java and suffer a post-traumatic syndrome from Jython?
My full code is below, sorry for being long.

Thanks to your great example code, I was able to just copy&paste it into the script editor and run it, but I wasn’t able to reproduce your issue. Everything worked fine with at least 100 clicks.

I was testing in an up-to-date Fiji installation with Java 8 on Windows 7. The jython-shaded.jar on my system is version 2.7.0 (search for jython with View all files in the advanced mode of the updater).

Hello, Jan,
Thanks for reply. The callback issue you mentioned looks exactly like mine. I am using jars/jython-shaded-2.5.3.jar version, older than yours, but my FIJI thinks it is up-to-date I followed the instructions in issue with callbacks in Jython as you pointed, replaced my old jars/scripting-jython-0.2.4.jar with the latest one, and it worked like a charm.
Many thanks!

I had exactly same issue. It is related to garbage collector issues with Jython interpreter. Here is what i heard from Curtis Rueden:

This is a known issue when your Python code registers callbacks which survive the execution of the script itself.

There is some rather aggressive reference cleaning logic which tries to null things out as soon as the script execution completes. Without such logic, Jython scripts end up with huge memory leaks, since references within the script are retained permanently.

A while back, I briefly played around with removing the reference cleaning logic but then the memory leaks are back.

So in short: you have stumbled upon a mammoth bug in the SciJava Jython scripting support, for which I do not currently have a good solution, apart from “use a different script language”. Sorry.

But two weeks later - after he’s done some testing with new release of scripting-jython - the issue seemed to be resolved. I do not know if this version of scripting-jython in already on the update site, but you can just download it, delete old one from jars folder and drop in the new one.

Hello Radoslaw,
I encounter a similar problem. When executing a jython script it randomly crashes and imageJ claims that imported libraries are not available. This also happens with the latest FiJi installation (June2017).
For example performing a channel split with ChannelSplitter can fail after several successful runs with the error