19

I've got a program that's having some trouble during shutdown, raising exceptions that I can't trace back to their source. It appears to be timing-related and non-deterministic. This is occurring after all shared resources have been released, and since it's shutdown, memory leaks are not an issue, so that makes me wonder if there's any way to just tell the program to terminate immediately and silently after releasing the shared resources, instead of continuing with the shutdown sequence and giving an exception message box.

Does anyone know how to do that?

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • 1
    What kind of program is? Console app? TApplication based? You are trying to debug /into/ the shutdown process? – Nick Hodges Jul 26 '09 at 16:53
  • TApplication based, and yes I am, and it's not working. It seems to be timing-related, plus the debugger's more or less useless in this app. See http://qc.embarcadero.com/wc/qcmain.aspx?d=76039 for the reason. – Mason Wheeler Jul 26 '09 at 22:15

5 Answers5

19

After looking at the Delphi Run Time Library source code, and at the Microsoft documentation; I can corroborate Mason and Paul-Jan comments.

The hierarchy of shutdown is as follows

  Application.Terminate()
    performs some unidentified housekeeping of application
    calls Halt()

  Halt()
    calls ExitProc if set
    alerts the user in case of runtime error
    get rid of PackageLoad call contexts that might be pending
    finalize all units
    clear all exception handlers
    call ExitprocessProc if set
    and finally, call ExitProcess() from 'kernel32.dll'

  ExitProcess() 
    unloads all DLLs
    uses TerminateProcess() to kill the process
PA.
  • 28,486
  • 9
  • 71
  • 95
  • `TApplication.Terminate()` does not call `Halt()`. It calls a list of termination procs, if set, and then posts a `WM_QUIT` message to the calling thread's message queue. When `TApplication.Run()` picks up the `WM_QUIT`, it breaks its message loop and exits, returning to the project's main code block which then exits, triggering process cleanup. – Remy Lebeau May 24 '15 at 08:37
6

ExitProcess(0) ?

HeartWare
  • 7,464
  • 2
  • 26
  • 30
4

Halt(0) used to be the good old fashioned way of telling the program to end with immediate effect. There's probably a more Delphi-friendly way of doing that now, but I'm 95% sure halt(0) still works. :-)

robsoft
  • 5,525
  • 4
  • 35
  • 47
  • 1
    It definitely works, but it will still do a nice and clean processing of all finilization parts and exitprocs. If one of those triggers Mason's issue, he is still in trouble. – Paul-Jan Jul 26 '09 at 17:09
4

In case HeartWare's suggestion of using ExitProcess() fails, it might be you are using some DLL's that do not respond well to the DLL_PROCESS_DETACH. In that case, try using a TerminateProcess( getCurrentProcess, 0 );

Once you resort to such measures, one might wonder if the "cleanly" part of the topic title still holds up to scrutiny.

Paul-Jan
  • 16,746
  • 1
  • 63
  • 95
1

The last time I had to hunt a problem like this was the shutdown was a causing an event (resize? It's been a while.) to fire on the dying window causing an attempt to redraw something that needed stuff that had already been disposed of.

Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45