Thread: Stack trace for access violation

  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
    28,413
    A quick search of the Web brings up Mixing SEH and C++ Exceptions. Perhaps it would be useful in your context.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 03: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, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM