2

A c++ access violation error gets thrown at some part of my code. I would like to "catch" it and log the faulting line so I can investigate further. I tried the first answer in this thread: Catching access violation exceptions?

Unfortunately no luck so far. I am using debug mode in Visual Studio 2019. Anyone knows a way how to log the faulting line?

codefast
  • 37
  • 7
  • 1
    Don't catch it. Access violation means your program is broken. If you do intercept the signal, discard it, and continue, you can't know what state the program is in. – user4581301 Nov 30 '21 at 23:04
  • What you want to do is use Visual Studio's best-of-breed debugger to help you track for crash back to the bug that caused it and squash the bug flat. If you can show code, we'll gleefully farm your question for extra reputation by helping fix it. – user4581301 Nov 30 '21 at 23:06
  • Hello, thank you for answering. The error happens in release mode and I can't seem to find the line. I'm only "catching" it in order to understand where it's going wrong. – codefast Nov 30 '21 at 23:09
  • 1
    You should only ever catch these exceptions in order to generate a dump file, then terminate your program. Visual Studio had a `__try` `__except` mechanism for this. – ChrisMM Nov 30 '21 at 23:10
  • 1
    Related: [https://learn.microsoft.com/en-us/cpp/cpp/structured-exception-handling-c-cpp?view=msvc-170](https://learn.microsoft.com/en-us/cpp/cpp/structured-exception-handling-c-cpp?view=msvc-170) – drescherjm Nov 30 '21 at 23:10
  • 1
    https://stackoverflow.com/a/7049836/17034 – Hans Passant Nov 30 '21 at 23:15

2 Answers2

2

The "Access Violation", that you are referring to, cannot happen in well-formed code. It is always the result of undefined behavior.

When that happens the state of your program is unknown, and is corrupted in some unspecified and unknown way. You cannot expect to accomplish any meaningful task in the program, any more, because the state of the program is no longer known. I suppose that if the operating system protects the program's code segment read-only you could rely on the fact that the actual code is intact, but the stack is in unknown state, and you can forget all about the heap, its state is completely unknown.

The only meaningful means to extract any useful diagnostics from the flawed program is by using an external debugger to analyze and inspect the frozen snapshot of the program's state at that point, from an external view.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
2

On Windows, you can do it with __try ... __except, as in the following simplified example:

__try
{
    * (int *) 0 = 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
    std::cout << "Exception caught\n";
}

Microsoft call this mechanism 'structured exception handling' and it is much more powerful (and, indeed, usable) than signals on Unix and I encourage you to learn more about it if you want to do this kind of thing:

https://learn.microsoft.com/en-us/cpp/cpp/try-except-statement?view=msvc-170

I use it to catch exceptions in poorly behaved ASIO drivers which would otherwise crash my app.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Not a direct method for finding the point of failure, you're in the exception handler and possibly far, far away from the crash site, but should be helpful in playing a few rounds of divide and conquer to help you zoom in on the crash. Where the actual bug is... that's always a problem. – user4581301 Nov 30 '21 at 23:41
  • Hi Paul, thank you for answering. Suppose inside your __try clause you call another method and that one calls others. How woud I find the line that is throwing the error, which is what I am interested at and the main point of my question? Again, thank you for taking the time to answer. – codefast Dec 01 '21 at 02:16
  • 1
    https://stackoverflow.com/questions/48924449/reliable-way-to-print-exception-backtrace-in-catch-handler – Paul Sanders Dec 01 '21 at 07:40
  • I tried doing that in the filter method using booststacktrace() but no luck. – codefast Dec 01 '21 at 16:01
  • Actually I figured it out. This worked. Thank you. – codefast Dec 01 '21 at 16:35
  • Excellent, wd.. – Paul Sanders Dec 01 '21 at 20:02