Hi Georg, and all PJSR developers,
I have extremely good news
All garbage collection problems have been fixed. Simplifying, the problem was that SpiderMonkey's garbage collector was incorrectly collecting event handlers (which are Function objects and hence are subject to garbage collection). Unexpected destruction of event handlers was causing severe corruption of internal data structures, especially if the collector was invoked in response to an event (directly or indirectly). This is the reason why calling gc() from an onValueUpdated handler (or from other event handler indirectly invoked from onValueUpdated) was crashing.
The cause of these errors is that I wasn't
rooting event handler objects well. A
root object in the JavaScript engine means that the garbage collector will not collect it, nor any object depending on it. Since event handlers aren't children (methods) of any particular object, they need explicit rooting to prevent their accidental destruction. For example, in the following snippet:
function MyDialog
{
// ...
this.mySlider = new Slider( this );
with ( this.mySlider )
{
onValueUpdated = function( value )
{
// ...
};
}
}
this.mySlider.onValueUpdated is an
unrooted Function object. It is initially assigned to the
onValueUpdated property of
this.mySlider, but the JavaScript engine thinks that once it has been assigned, it is no longer used since there is no explicit call to it, and hence it is susceptible of garbage collection
as soon as the script begins execution.
Now all event handler functions are automatically rooted by the PJSR upon creation, and unrooted once the script terminates execution, just before calling the garbage collector to clean up the terminated script. This has fixed the problem definitely.
PixInsight version 1.5.3 adds a number of important new routines and properties to the PJSR. Among them:
Boolean Global.jsAbortable
This property is false by default. When a script sets it to true, the script becomes
abortable, that is, the user can interrupt its execution by clicking the Pause/Abort button on the Processing Console. For this option to work, the script must be running without an active modal dialog, since modal windows prevent interaction with other elements of the GUI. This option is good for debugging scripts that can enter infinite loops and the like.
Boolean Global.jsAutoGC
This property is false by default. When a script sets it to true, PixInsight enables an automatic, asynchronous garbage collection routine. With this option enabled, a script doesn't need to worry about explicit garbage collection because the PJSR performs it behind the scenes. The frequency of garbage collection attempts is proportional to the complexity of the running script. Asynchronous garbage collection has a very small though noticeable impact on execution performance (this is the reason why it is disabled by default).
With the new features and modifications, the PJSR is now extremely stable and doesn't have the old memory problems anymore. Real-time tasks are now fully supported in the PJSR, which is an important step forward.