Author Topic: Should view.endProcess() be in finally clause ?  (Read 4179 times)

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Should view.endProcess() be in finally clause ?
« on: 2015 February 01 13:28:49 »
I try to reuse some code of BPP.  I see the following sequence:

Code: [Select]
  var view = window.mainView;
   view.beginProcess( UndoFlag_NoSwapFile );
   if ( !f.readImage( view.image ) )
      throw new Error( "Unable to read file: " + filePath );
   window.keywords = f.keywords;
   view.endProcess();

Should the view.endProcess() not be in a finally close so it is processed even in case of error ? I guess it is not too important as the script likely exits, but is there a risk to keep lock memory in case of error?

 -- bitli

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Should view.endProcess() be in finally clause ?
« Reply #1 on: 2015 February 02 04:00:23 »
When a script terminates, View.endProcess() is invoked automatically for all View objects that require it. So it is actually much better to let the runtime decide how and when to call View.endProcess() upon a script termination caused by an uncatched exception.

However, View.endProcess() should be called by a script if it handles an exception in a situation like:

Code: [Select]
try
{
   view.beginProcess();
   processTheViewHere();
   view.endProcess();
}
catch ( x )
{
   view.endProcess();
   handleTheExceptionHere();
}

or even:

Code: [Select]
let beingProcessed = null;
try
{
   doSomethingThatCanThrowExceptions();
   view.beginProcess();
   beingProcessed = view;
   processTheViewHere();
   view.endProcess();
   beingProcessed = null;
   doSomethingElseThatCanThrowExceptions();
}
catch ( x )
{
   if ( beingProcessed )
   {
      beingProcessed.endProcess();
      beingProcessed = null;
   }
   handleTheExceptionHere();
}
Juan Conejero
PixInsight Development Team
http://pixinsight.com/