Shutdown Hook C ++

Is there any way to run the code on completion, regardless of what kind of completion (abnormal, normal, uncaught exception, etc.)? I know this is possible in Java, but is it possible in C ++? Im assuming a windows environment.

+5
source share
4 answers

No - if someone calls TerminateProcess, your process will be destroyed without further adieu and (in particular) without any chance of running another code during the shutdown process.

+5
source

For a normal closing application, I would suggest

atexit()
+2
source

++ RAII, , , ..

class ShutdownHook {
  ~ShutdownHook() { 
    // exit handler code 
  }
}; 

int main() { 
  ShutdownHook h; 
  //...
} 

Object Lifetime Manager ACE. atexit.

+1

- ; , , , KILL Linux.

, , , , , .

0

All Articles