locked
How to catch Access violation exception RRS feed

  • Question

  • Hi,

    My code throws access violation exception in strncpy function, because it tries to copy the data at an invalid memory location. I removed that bug but the thing is that i tried to catch this exception using try-catch and also through try-except, but in vain.
    Actually i am developing a dll for use as extended stored procedure in Sql Server 2005. When this type of exception occurs, it crashes the Sql Server service which is very serious issue.
    So at my end, I can avoid this exception but how to avoid it?

    Please tell me how to prevent that situation.

    Regards,

    Mazhar Nazeer.

    Monday, July 6, 2009 1:15 PM

Answers

  • If a program experiences an access violation error, then your program is in an unknown state and can behave unpredictably if permitted to continue. The proper course of action it to stop the entire program and remove it from memory. If your code is in a DLL, then the entire program needs to be stopped, and that means SQL Server service in your case. You may consider this a "serious issue", but it is more serious to permit further execution rendering erroneous results.

    The rule is this: If you program has a bug, excise the bug. Don't try to paper over it.

    BTW, to catch an access violation you need to use Structured Exceptions. For reasons discussed, they are of little practical value.
    • Marked as answer by nobugz Monday, July 6, 2009 4:55 PM
    Monday, July 6, 2009 3:59 PM

All replies

  • If a program experiences an access violation error, then your program is in an unknown state and can behave unpredictably if permitted to continue. The proper course of action it to stop the entire program and remove it from memory. If your code is in a DLL, then the entire program needs to be stopped, and that means SQL Server service in your case. You may consider this a "serious issue", but it is more serious to permit further execution rendering erroneous results.

    The rule is this: If you program has a bug, excise the bug. Don't try to paper over it.

    BTW, to catch an access violation you need to use Structured Exceptions. For reasons discussed, they are of little practical value.
    • Marked as answer by nobugz Monday, July 6, 2009 4:55 PM
    Monday, July 6, 2009 3:59 PM
  • Hello Brian,

    Thanks for you very helpful answer.

    I tried to use the Structured Exceptions such as __try-__except, but it didn't caught the AV exceptions. But MSDN tells that it should caught them.

    Please let me know if there is some other way out. I just don't want that SQL Server crash.

    Monday, July 13, 2009 10:06 AM
  • Not sure what you are doing wrong. There is a simple coding example found here. Why don't you give it a try?

    By the way, did you get rid of the original bug?
    Monday, July 13, 2009 7:02 PM
  • Yes i fixed the original bug due to which this exception occurs.

    But i am conscious about the future, that what if this type of exception occurs in future.

    I followed the same example for __try-__except, but it didn't catch AV exceptions. I also tried try-catch but in vain.

    I think there is no other way to catch these exceptions....as these are hardware exceptions and C++ couldn't catch them.

    What you think?
    Tuesday, July 14, 2009 4:44 AM
  • As Brian already said, your app is hopeless after an access violation. :) As a last resort you can probably log some kind of a message or stack dump etc. See StackWalker sample in codeproject. Also have a look at functions AddVectoredExceptionHandler , SetUnhandledExceptionFilter . These are user defined handlers for unhandled exceptions.


    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    Posts are provided as is without warranties or guaranties.
    • Edited by Nibu Thomas Tuesday, July 14, 2009 10:57 AM
    Tuesday, July 14, 2009 10:56 AM
  • Dear Thomas,

    Thanks for the StackWalker, it's really helpful. But one thing is again happening due to which i can't use this. Actually when AV exception occurs, it didn't go to catch or __except block. It just pops out from SQL Server and shows the win32 exception. I compiled it with /SHa option.
    Wednesday, July 15, 2009 4:50 AM
  • That's why I gave the link to the other two functions. ;)

    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    Posts are provided as is without warranties or guaranties.
    Wednesday, July 15, 2009 5:31 AM
  • A bit late, but oh well...

    If structured exception handling isn't capturing the access violation it is probably because the stack was trashed and thus there was no possible way for the exception hanlding code to find your catch block.  If you had simply referenced some memory that was inaccessible (for example, by using a bad pointer) then the __try/__except would have caught the access violation.

    Thursday, June 10, 2010 9:42 PM
  • Hi Paul,

    i tried the structured exception by creating a dynamic array

    int *p = new int[10];

    i did a loop to see when the AV will throw, & got the AV when the counter was 44 in Release mode, and 385 in Debug mode. 

    i understand that the debug mode permits some array access violation to be denied, but this shouldn't happen in Release.

    Thanks

    Code :

     

    void main()

    {

    int *p = new int[10];

    int sizeP = TryExcept(p);

    return 0; 

    }

    int filter(unsigned int code, struct _EXCEPTION_POINTERS *ep)

    {

       if (code == EXCEPTION_ACCESS_VIOLATION) {

          puts("caught AV as expected.");

          return EXCEPTION_EXECUTE_HANDLER;

       }

     

       else {

          puts("didn't catch AV, unexpected.");

          return EXCEPTION_CONTINUE_SEARCH;

       }

    }

     

    int TryExcept(int *p)

    {

    int size = 0;

    for(int i=0;; i++)

    {

    __try{

     __try{

     p[i] = 13;    // causes an access violation exception;

    int test = p[i];

    size++;

     }__finally{

     }

    }__except(filter(GetExceptionCode(), GetExceptionInformation())){

     return size;

    }

    }

    return size;

    }

    Wednesday, June 30, 2010 10:25 AM
  • Hi did a loop to see when the AV will throw, & got the AV when the counter was 44 in Release mode, and 385 in Debug mode. 

    i understand that the debug mode permits some array access violation to be denied, but this shouldn't happen in Release.


     

    You seem to have the mistaken belief than an access violation occurs whenever you access memory beyond the end of an array. In fact, you need to try to access a segment of memory that does not belong to your program. That's a big difference. Read up on access violation.

    Wednesday, June 30, 2010 4:30 PM
  • Hi Brian,

    thank you for the link you gave, it's very helpfull.

    i added the exception : EXCEPTION_ARRAY_BOUNDS_EXCEEDED, to try to catch the buffer overflow before, i still get the exception on EXCEPTION_ACCESS_VIOLATION

    Does EXCEPTION_ARRAY_BOUNDS_EXCEEDED work only with CLR/ managed C++/array ?

    Friday, July 2, 2010 9:16 AM