Thread: why my termination routing is not called when program crashes

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    16

    why my termination routing is not called when program crashes

    I recently upgraded my project from VC++ 6.0 to VC++ 2005. I am trying to define some error logging in my custom termination routing. And here is what I did:

    1. define
    Code:
    void term_func()
    {
    // some custom processing
        exit(-1);
    }
    2. put
    set_terminate( term_func );
    somewhere in code that is definitely executed.

    3. I have no "try-catch" in the entire program.

    However when the program crashes --- due to a condition where fwrite to a null pointer --- term_func doesn't seem to be executed.

    Does anyone know why? Should I change some of the project/compile properties?

    Thanks in advance!

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Are you running it through the debugger? According to msdn, set_terminate only works outside the debugger.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    16

    made sure it is outside the debugger, still no luck

    Well, here is what I did:

    1.
    Code:
    void term_func()
    {
    	cout << "term_func() was called by terminate().\n";
    
        exit(-1);
    }
    2. put
    set_terminate( term_func );
    somewhere in the program which executes early

    3. compile the code in "release" mode with /EHsc;

    4. open a console, type:
    test1.exe > a.out

    5. the program is a dialog window, when it crashes, nothing shows up in a.out

    Anyone knows why?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why not use the standard C function atexit()?

    Make the simplest program possible that demonstrates that. Does this do the same thing?
    Code:
    #include <stdio.h>
    
    int main(void) {
        puts("before");
        set_terminate(term_func);
        puts("after");
        return 0;
    }
    If that doesn't work, try atexit().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    I believe that since a segmentation fault occured, the OS closes your application without asking it to terminate because it is trying to access memory it does not have the permission to access.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program crashes + fscanf() question
    By happyclown in forum C Programming
    Replies: 27
    Last Post: 01-16-2009, 03:51 PM
  2. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 10:27 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. Program crashes and I can't figure out why
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-19-2001, 05:33 PM