Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch the "Access violation reading location 0x00000000" exception

I'm using a method from 3rd party dll and it throws "Access violation reading location 0x00000000" exception. I cannot dig in so I'm only wondering if there is anyway to catch it so not collapse the application. I tried the following 4 methods but none of them works.

1,

try
    {
    sts = resFilter->initialize(m_JPEG2000File); // it throws that exception
    }
    catch (...){
        printf("Gotcha0...");
        int a = 34;
    }

2, 3 and 4

LONG WINAPI CrashHandler1(EXCEPTION_POINTERS * a/*ExceptionInfo*/)
{  std::cout << "Gotcha1!" << std::endl;
return 0;
}

void CrashHandler2()
{    std::cout << "Gotcha2!" << std::endl;}

void CrashHandler3()
{    std::cout << "Gotcha3!" << std::endl;}

// in Main()
::SetUnhandledExceptionFilter(CrashHandler1);
std::set_terminate (CrashHandler2);
std::set_unexpected( CrashHandler3 );

Test(); // It would throw "Access violation reading location 0x00000000" exception 

If I debug it, exception would be thrown. If I run it in run time, "Gotcha1!" would be displayed in the console but the application would still collapse. Is there any way I can eat this exception?

Thanks in advance,

Ben

Edit:

@Adriano Repetti mentioned __try and __except can catch this exception.

Thanks for all you guys heads-up for not eating that exception!

I have an external C# executable calling this project. I want to catch this exception so I have chance to log the error and do not collapse the C# application. I would still terminate this very c++ process. I'm looping the data in C# which would start a new C++ process from scratch every time, so it would be a new C++ instance. So Adriano's approach works for me.

like image 644
Ben Avatar asked Jun 12 '15 14:06

Ben


2 Answers

Do not "eat" access violations; that way abject madness lies.

It seems to me that you're trying to dereference a null pointer. Perhaps simply do not do that!

if (!resFilter) {
   // do something else; e.g. did you fail to initialise the library properly?
   throw "Yikes!";
}

// OK; pointer is not NULL at least: let's go!
sts = resFilter->initialize(m_JPEG2000File);

Now, if you get an access violation from that, then the library is exceedingly buggy and you should cease using it immediately unless a fix or patch is available.

Since you're on Windows, if all you want to do is detect the problem for logging purposes, you can use Visual Studio's non-standard __try/__catch construct, but be sure to immediately terminate the process after logging the problem, because your process (particularly the state of the library) will be unstable and nothing you do with it after that will have any meaning!

__try  { 
    sts = resFilter->initialize(m_JPEG2000File);
} 
__except(
   GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
   ? EXCEPTION_EXECUTE_HANDLER
   : EXCEPTION_CONTINUE_SEARCH) { 
    std::cerr << "OMG!\n";
    exit(-1);
}
like image 84
Lightness Races in Orbit Avatar answered Oct 25 '22 04:10

Lightness Races in Orbit


__try and __except can catch this exception. Thanks to @Adriano Repetti!

Here is a good post about it: C++, __try and try/catch/finally

like image 1
Ben Avatar answered Oct 25 '22 06:10

Ben