Recent

Author Topic: Proper way to terminate the program  (Read 12722 times)

riwu

  • New Member
  • *
  • Posts: 15
Proper way to terminate the program
« on: January 07, 2017, 03:01:11 am »
My program does not provide an Application object, so the proper way according to http://docwiki.embarcadero.com/Libraries/XE8/en/System.Halt is to call Exit from main Program block.

However, the program might need to be terminated in deeply nested routines, hence coding immdiate checks in each routine call to exit all the way to the main Program block is a lot of work, so i thought of 2 ways:
1. call Abort and let the exception propagate to the main Program block, which will have a try block to catch EAbort and exit the program.
2. call Halt.

Which of the above is preferred and why?

Quote
Halt performs an abnormal termination of a program and returns to the operating system.
What constitute 'abnormal termination'? Anything besides successful and complete execution of the program?
Are user input error, resource access (file, web) error etc considered requiring abnormal termination?

If i am not using Application object, what kind of resources might possibly not be freed by calling halt, since finalization block is still executed?

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Proper way to terminate the program
« Reply #1 on: January 07, 2017, 04:06:23 am »
Which of the above is preferred and why?

You're looking at Delphi docs. FPC docs for Exit and Halt are here:

http://www.freepascal.org/docs-html/rtl/system/exit.html
http://www.freepascal.org/docs-html/rtl/system/halt.html

If you don't have an Application object, then that means you're not writing a GUI LCL app, since all LCL apps have a global Application object. Maybe you're writing a console app?

With a console app, just call Halt, passing an error code if it's a halt that you want to detect, say, in the script or batch file that called the app.

Note that a properly designed program should already be able to exit gracefully. Eg, functions should return a success or failure Boolean result indicating that and calling function can detect failure and in turn exit with failure code, etc. Topmost level then can use Exit (or Halt). If it's a lot of work to exit like this in your program, then perhaps you didn't design your functions very well.

You could also raise an exception that won't be trapped, if you want that displayed instead, eg, in the console.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Proper way to terminate the program
« Reply #2 on: January 07, 2017, 04:19:09 pm »
Which of the above is preferred and why?
Clean: 1
Dirty: 2
Quote
Halt performs an abnormal termination of a program and returns to the operating system.
What constitute 'abnormal termination'? Anything besides successful and complete execution of the program?
Are user input error, resource access (file, web) error etc considered requiring abnormal termination?
Abnormal in the sense of no cleanup is done at all. Assume Halt as a goto to the end of your program from wherever it is at the moment.
If i am not using Application object, what kind of resources might possibly not be freed by calling halt, since finalization block is still executed?
What you've allocated, both explicitly and implicitly.

riwu

  • New Member
  • *
  • Posts: 15
Re: Proper way to terminate the program
« Reply #3 on: January 08, 2017, 06:20:03 am »
Which of the above is preferred and why?
Clean: 1
Dirty: 2
Quote
Halt performs an abnormal termination of a program and returns to the operating system.
What constitute 'abnormal termination'? Anything besides successful and complete execution of the program?
Are user input error, resource access (file, web) error etc considered requiring abnormal termination?
Abnormal in the sense of no cleanup is done at all. Assume Halt as a goto to the end of your program from wherever it is at the moment.
If i am not using Application object, what kind of resources might possibly not be freed by calling halt, since finalization block is still executed?
What you've allocated, both explicitly and implicitly.
I just checked the implementation of Halt in FPC 3.0, Halt() calls InternalExit() which finalizes units and releases the heap, so for FPC it should be safe to call Halt() to terminate a console program?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Proper way to terminate the program
« Reply #4 on: January 08, 2017, 06:54:33 am »
I just checked the implementation of Halt in FPC 3.0, Halt() calls InternalExit() which finalizes units and releases the heap, so for FPC it should be safe to call Halt() to terminate a console program?
It's always safe so long as there are no allocated resources linked to 3rd party services requiring explicit deallocation. Memory leaks on dead programs will be reclaimed by the OS, but if you allocate a handle to something else (say, a file based database access lock), I don't think it has the same level of guarantee.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Proper way to terminate the program
« Reply #5 on: January 08, 2017, 07:40:45 am »
Safely removing allocated resources (on halt):
Code: Pascal  [Select][+][-]
  1. program halter;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. procedure DeAllocResourceOne;
  6. begin
  7.   WriteLn('Deallocate Resource One');
  8. end;
  9.  
  10. procedure DeAllocResourceTwo;
  11. begin
  12.   WriteLn('Deallocate Resource Two');
  13. end;
  14.  
  15. function AllocResourceOne: boolean;
  16. begin
  17.   Result := True;
  18.   WriteLn('Allocate Resource One');
  19. end;
  20.  
  21. function AllocResourceTwo: boolean;
  22. begin
  23.   Result := True;
  24.   WriteLn('Allocate Resource Two');
  25. end;
  26.  
  27. begin
  28.   if AllocResourceOne then
  29.   begin
  30.     AddExitProc(@DeallocResourceOne);
  31.     Halt;
  32.     if AllocResourceTwo then
  33.     begin
  34.       AddExitProc(@DeallocResourceTwo);
  35.     end;
  36.   end;
  37. end.
  38.  

Or you wrap it all up into one exitproc checking each resource manually, acting accordingly. The latter is then becoming a standard 'panic' routine. A bit of wiggling with some code delivers you a (optional) panic message along with the routine name and line number from which the panic routine was called from.

Never ever call halt without releasing allocated resources. It's gonna bite you in the end.
« Last Edit: January 08, 2017, 07:54:11 am by molly »

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: Proper way to terminate the program
« Reply #6 on: January 08, 2017, 06:36:55 pm »
Does Halt execute the code in try..finally blocks?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Proper way to terminate the program
« Reply #7 on: January 08, 2017, 07:02:28 pm »
Does Halt execute the code in try..finally blocks?
No, depending on what you exactly meant with that question.

To generate an exception you can use Abort (and yes that can be combined with halt/exitproc).

 

TinyPortal © 2005-2018