CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2008
    Posts
    5

    Catching ACCESS_VIOLAION in Vectored Exception Handler

    Hello all,

    I'm having some trouble understanding why an ACCESS violation isn't caught in my exception handler.

    I add the handler with:
    --
    m_pvExceptionHandle = AddVectoredExceptionHandler( 1, &myExceptionHandler );
    LONG CALLBACK myExceptionHandler( PEXCEPTION_POINTERS ExceptionInfo );
    --

    But, after adding this.. the following code crashes the application(like it normally should):
    char* pTest = NULL;
    *pTest = 'v';

    I understand that a Win32 Exception is not a normal c++ exception... so I could prevent the program crash here by placing the *pTest = 'v'; line in a try/catch block and compiling the code with /EHa.
    But I want to be able to catch an access violation from anywhere in the application ( so 3rd party libraries as well ).

    If this is possible... is it then also possible to 'resume' execution? (basically returning EXCEPTION_CONTINUE_EXECUTION from myExceptionHandler).

    I suspect that it is 'just' a compiler option that will enable me to catch the access violation, rather than crash the application but I have tried what I know about.


    If this isn't possible... is there then another way to allow certain pages to be executable, but not read/writeable?

    I (sortof) know about PAGE_GUARD... but this will fire even if the access method was execute... and filtering this isn't really acceptable (the pages get executed often).

    I thought about setting hardware breakpoints... but this limits to only 4 addresses.

    Thank you in advance for the help.

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: Catching ACCESS_VIOLAION in Vectored Exception Handler

    Quote Originally Posted by thelamb View Post
    If this is possible... is it then also possible to 'resume' execution? (basically returning EXCEPTION_CONTINUE_EXECUTION from myExceptionHandler).
    In theory: yes. In practice: i don't think it's possible in your case. EXCEPTION_CONTINUE_EXECUTION makes the running thread execute again the command wich raised an exception. Therefore, if you return EXCEPTION_CONTINUE_EXECUTION you should be sure you really handle a problem situation and this exception won't be raised again. Otherwise you'll get an infinite loop. And I don't think it's possible to do this with any exception in an application.

    It's hard to say why your program crashes without seeing the definition of the myExceptionHandler. Probably you return EXCEPTION_CONTINUE_SEARCH and you have no other exception handlers.

  3. #3
    Join Date
    Nov 2008
    Posts
    5

    Re: Catching ACCESS_VIOLAION in Vectored Exception Handler

    Hello Nikitozz,

    Thank you for your reply.

    The problem is, that on ACCESS_VIOLATION the handler does not even get called, it straight crashes the application as if the handler was not there.

    I know that, after the handle gets called I need to be sure that I fixed the problem before telling it to resume execution... and I will be sure, else not return CONTINUE_EXECUTION.
    But it's not even calling the handler so...

  4. #4
    Join Date
    Dec 2008
    Posts
    144

    Re: Catching ACCESS_VIOLAION in Vectored Exception Handler

    Hmm. In my program the handler gets called.
    Code:
    #define _WIN32_WINNT 0x0501
    
    #include <windows.h>
    
    LONG CALLBACK myExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
    {
    	return EXCEPTION_CONTINUE_EXECUTION;
    }
    
    int main(int argc, char* argv[], char* env[])
    {	
    	AddVectoredExceptionHandler( 1, &myExceptionHandler );
    
    	char* pTest = NULL;
    	*pTest = 'v';
    
    	return 0;
    }
    It's just an infinite loop but I got expected results.
    I build it in VS2005 with default project settings.

  5. #5
    Join Date
    Nov 2008
    Posts
    5

    Re: Catching ACCESS_VIOLAION in Vectored Exception Handler

    Ok I use the VS2008 compiler (there have been some changes regarding exceptions, especially win32).

    But maybe I've fiddled around with the options too much... I'll try from a clean project when I have the time.

    Thank you for taking the time though

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured