none
How are objects disposed when an application is abruptly/suddenly terminated? RRS feed

  • Question

  • Good day!
    I created a simple application with one class having a destructor and another that implements the IDisposable interface. In both the destructor and the Dispose() method, I do some cleanup, specifically deletion of a file to test if the methods are called. But why is it that when an application is abruptly terminated, the destructor or the Dispose() is not called? Is there a way to ensure the cleanup when an application is not allowed to exit gracefully (like 'killing the process')?

    Thank you very much.
    Monday, July 14, 2008 9:16 AM

Answers

  • There is no way.  TerminateProcess() abruptly aborts all threads in a process without a chance for cleanup.  Not unlike a power outage.  Make your app resilient to such mishaps.  For example, by checking that left-over files are present at startup.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Friday, July 18, 2008 5:44 AM
    Monday, July 14, 2008 12:01 PM
    Moderator

All replies

  • When an application is terminated abruptly, all the memory allocated for that process is returned to the Operating System instantly. So, that memory includes all the objects on the heap and stack. Thats why, any code inside the process cannot be executed.
    Usually, under this circumstances, there will not be any memory leak, but it can result in corrupted files.

    --
    Madhur

    http://blogs.msdn.com/mahuja
    Monday, July 14, 2008 10:14 AM
  • There is no way.  TerminateProcess() abruptly aborts all threads in a process without a chance for cleanup.  Not unlike a power outage.  Make your app resilient to such mishaps.  For example, by checking that left-over files are present at startup.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Friday, July 18, 2008 5:44 AM
    Monday, July 14, 2008 12:01 PM
    Moderator