Stack trace for access violation

This is a discussion on Stack trace for access violation within the C++ Programming forums, part of the General Programming Boards category; I am trying to write some general exception-handling logic for my C++ code, so that for *any* exception I log ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    Unhappy Stack trace for access violation

    I am trying to write some general exception-handling logic for my C++ code, so that for *any* exception I log a stack trace to a file, and backup some data before the app crashes.

    I am using an access violation as a test case, as in the following code:

    Code:
    try{
    *((char*) 0) = 1;
      } catch (exception &e) {
    	  recorderror(e);
      } catch (...) {
    	  logerror("exception caught");
      }
    First, I had to set the /EHa flag in Visual Studio to enable this exception to get caught at all. But now it is getting caught at the 'catch (...)' where I seemingly have no information about the exception.

    Inside the 'catch (...)', how can I determine what type of exception was caught and how can I generate the stack trace? I did some searching around and can't seem to find a solution.

    I found a way to get the stack trace at the point inside the catch, but that doesn't help because this is code that is going to enclose my entire program. I need the stack trace from the point of the original exception.


    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    A quick search of the Web brings up Mixing SEH and C++ Exceptions. Perhaps it would be useful in your context.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by locksleyu View Post
    I am trying to write some general exception-handling logic for my C++ code, so that for *any* exception I log a stack trace to a file, and backup some data before the app crashes.
    Logging a stack trace is a good idea. I think backing up data is a bad one. Once your program has received an exception (and I mean SEH, not C++ exceptions which are synchronous) there is absolutely no way of telling just how badly things are screwed up.

    You might think you are backing up some critical data, but might instead be overwriting the backup (which was intact) with complete garbage. The worst thing a crashing program can do is take down other programs and data along with it.

    If you've already decided you're going to crash, then just crash immediately. If the data is that critical, the app should be regularly backing it up during NORMAL execution, not once things go wrong.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21