Author Topic: Script Dialog - Multiple PushButton Presses Causing Memory Errors  (Read 891 times)

Offline dcarr

  • Newcomer
  • Posts: 20
Hello

In writing a script-based dialog recently (see link below), I noticed a couple of issues. I will mention them in separate posts to keep these matters distinct. I apologize in advance if the behaviors I describe are not in fact bugs but simply part of the PixInsight design that the script-writer must work within.

https://pixinsight.com/forum/index.php?topic=12643.0

As normal, when pressing a dialog pushbutton, a function may be called. In my case, the function creates images and performs some PixelMath. However, if the user pressed the same pushbutton too quickly, i.e. before the function completed running from the previous call, the function terminated immediately and started again (or at least appeared to do so in the Process Control window). In my case, this left the function and all sorts of data structures in undefined states, leading to memory errors and eventually to the collapse of PixInsight (requiring a reload of PixInsight).

My preference when writing a script is for me not to cause PixInsight to explode. So I have used a variable to act as a type of semaphore to prevent the function from executing unless the previous call has completed its run. No more problem, at least for now, but is this the correct solution?

So, is this a problem that needs fixing, have I made an error, or is this something the script-writer must work within in a multithreaded environment?

Regards
Dean


« Last Edit: 2018 September 05 17:41:26 by dcarr »

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Script Dialog - Multiple PushButton Presses Causing Memory Errors
« Reply #1 on: 2018 September 06 05:25:48 »
Hi Dean,

Quote
So, is this a problem that needs fixing, have I made an error, or is this something the script-writer must work within in a multithreaded environment?

No problem at all, just the expected behavior. This happens because you are executing a process instance, PixelMath in particular, which allows the platform to consume pending UI events from the core application's event queue during process execution. This is done to keep the user interface reasonably responsive during long process executions. If your interface allows triggering new events (such as button clicks) while the PixelMath process is running, you may expect all kinds of odd behaviors unless you design and implement your code very carefully to allow reentrant calls—which is a rather complex task for nontrivial scripts.

Your approach with a sentinel (bCombineImagesRunning) to block reentrant event calls should work. A simpler and probably safer solution to this problem is disabling your entire script's dialog while the process(es) are running, then re-enable it when finished:

   this.preview_Button.onClick = function()
   {
      this.dialog.enabled = false;
      combineImages( g_data, false /* not finalised */ );
      this.parent.scaledPreviewControl.updateSettings();
      this.dialog.enabled = true;
   };


You can also hide the dialog window during the process by calling hide() and show(). A disabled or hidden control cannot receive UI events.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline dcarr

  • Newcomer
  • Posts: 20
Re: Script Dialog - Multiple PushButton Presses Causing Memory Errors
« Reply #2 on: 2018 September 06 13:12:26 »
Thank you Juan

Your quick and very specific advice is greatly appreciated. Very helpful.

Regards
Dean