0

I've a C# WPF application that throws following error message and crashes:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I then added following exception handlers in the code. And also added following line of code in the start of the method which was throwing above exception.

throw new System.AccessViolationException();

Exception handlers are now getting executed on clicking the app exe on my local machine when above exception occurs, but the app still crashes. I need to be able to prevent the app from crashing. Not sure how do I do that?

application.DispatcherUnhandledException += ApplicationDispatcherUnhandledException;

TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;


AppDomain currentAppDomain = AppDomain.CurrentDomain;
currentAppDomain.UnhandledException += new UnhandledExceptionEventHandler
                        (UnhandledExceptionHandler);

Thanks.

Sam
  • 45
  • 8
  • 3
    By fixing the Access Violation. – Mark Benningfield Feb 28 '18 at 13:22
  • 1
    The **stack trace** of the exception will surely tell you _where_ the exception occured. Check that code, you are obviously doing something wrong that's corrupting your memory (maybe some native or p/invoke call). Try to fix that. We can't tell what you're doing wrong without seeing the (_relevant_) code. – René Vogt Feb 28 '18 at 13:24
  • You dont want it to crash, wrap it in `try catch`, but still it wont fix your issue. – Trevor Feb 28 '18 at 13:27
  • Answer here: https://stackoverflow.com/questions/7803168/how-do-i-continue-running-after-an-unhandled-exception – Marco Feb 28 '18 at 13:28
  • Can you show the content of `ApplicationDispatcherUnhandledException`? – LordWilmore Feb 28 '18 at 13:48

1 Answers1

1

You need to identify what part of code is throwing that exception. I bet it is some external unmanged library.

If you cannot repair that library, you can follow instructions on MSDN:

AccessViolationException

AccessViolationException and try/catch blocks

Starting with the .NET Framework 4, AccessViolationException exceptions thrown by the common language runtime are not handled by the catch statement in a structured exception handler if the exception occurs outside of the memory reserved by the common language runtime. To handle such an AccessViolationException exception, you should apply the HandleProcessCorruptedStateExceptionsAttribute attribute to the method in which the exception is thrown. This change does not affect AccessViolationException exceptions thrown by user code, which can continue to be caught by a catch statement. For code written for previous versions of the .NET Framework that you want to recompile and run without modification on the .NET Framework 4, you can add the element to your app's configuration file. Note that you can also receive notification of the exceptions if you have defined a handler for the AppDomain.FirstChanceException or AppDomain.UnhandledException event.

apocalypse
  • 5,764
  • 9
  • 47
  • 95