7

In C++, is there a standard way (or any other way, for that matter) to catch an exception triggered by a memory access violation?

For example, if something went wrong and the program tried to access something that it wasn't supposed to, how would you get an error message to appear saying "Memory Access Violation!" instead of just terminating the process and not showing the user any information about the crash?

I'm programming a game for Windows using MinGW, if that helps any.

Charles
  • 50,943
  • 13
  • 104
  • 142
rsethc
  • 2,485
  • 2
  • 17
  • 27
  • `try ... catch` perhaps? – Roger Rowland May 17 '13 at 15:13
  • 1
    Why would someone want to prevent a berserk program which try to modify protected memory from terminating ? – lucasg May 17 '13 at 15:13
  • 1
    Typically you don't "catch" these, you fix them from happening in the first place by running them through a debugger, such as GDB. – hexist May 17 '13 at 15:19
  • 2
    I think he just want to show a neat exit window instead of default OS window, then let it terminate anyway. Nothing wrong with that. – Cyrille May 17 '13 at 15:21
  • Yes, Cyrille that is exactly what I want to do. So that in case of a bug the user doesn't just get a crash to desktop and go "What just happened?". – rsethc May 17 '13 at 15:23
  • 1
    There's no general way to recover from that. Since you don't even know whether the call stack is sane at that point, throwing a C++ exception (which must unwind the stack and destroy relevant objects) isn't really possible. Besides, what would you write in the handler that you're *certain* is safe? – molbdnilo May 17 '13 at 15:24
  • 1
    In the interest of clarity: from a C++ perspective, accessing unallocated memory is undefined behaviour, not an exception. There may or may not be an exception at the operating system level, e.g. an access violation in Windows or a segmentation fault in Linux. You may be able to signal those as zakinster suggested. – smocking May 17 '13 at 15:24
  • [This](http://stackoverflow.com/questions/2663456/write-a-signal-handler-to-catch-sigsegv) is a nice related thread. – Anton Belov May 17 '13 at 15:29
  • 1
    Please note that tags stand alone. Combining `memory`, `access` and `violation` doesn't mean you're talking about a memory access violation. – Charles May 17 '13 at 17:25

2 Answers2

12

Access violation is a hardware exception and cannot be caught by a standard try...catch.

Since the handling of hardware-exception are system specific, any solution to catch it inside the code would also be system specific.

On Unix/Linux you could use a SignalHandler to do catch the SIGSEGV signal.

On Windows you could catch these structured exception using the __try/__except statement.

Akanksh
  • 1,300
  • 8
  • 10
zakinster
  • 10,508
  • 1
  • 41
  • 52
  • Is it possible to have a 'main' process that launches the actual app and then shows the error message based on the return code from the crashed app process? – rsethc May 17 '13 at 15:22
  • 1
    @rsethc That seems reasonably workable if they are two different processes. – zakinster May 17 '13 at 15:27
  • 1
    I think it might also be worth mentioning that there are systems without MMU (or MPU) that cannot detect a memory access violation at all. –  May 17 '13 at 15:31
5

This doesn't quite help you, but access violations and other SEH exceptions can be caught in MSVC using try...catch(...), if you compile with /EHa:

MSVC Yes with SEH exceptions (/EHa)

Live demo

#include <iostream>
int main() {
  try {
    *(int*)0 = 0;
  }
  catch (...) {
    std::cerr << "Exception!\n";
  }
}

Output:

Exception!

The only downside is you have no way of knowing what went wrong. To know more about the exception you can set a custom SEH translator function with _set_se_translator like in this article.

rustyx
  • 80,671
  • 25
  • 200
  • 267