Odd problem with System.AccessViolationException

I have an asp.net web application (3.5). On one page, I use the unmanaged C library (simulator), which creates a result set based on some input.

When I set debug="false" in web.config (Release mode), this call will System.AccessViolationException . If I'm in the debugger, or at least set debug="true" in web.config, this will never be an exception.

Due to the nature of the error, I placed a call inside try-catch to register an exception, and then the exception does not appear!

Is there any magic work on protected memory space in debug mode and / or in try-catch?

+4
source share
1 answer

AccessViolationException in this context probably means that your unmanaged C code is being twisted. It rewrites a bunch or something like that.

Keep in mind that many snippets of code have never been written to run in a multi-threaded environment such as ASP.NET. They were never even tested in such an environment, or were only tested using some kind of multi-threaded desktop form that does not apply to ASP.NET.

I had such a problem once. A third-party DLL that claimed to be thread safe was very dangerous. By attaching an unmanaged debugger to an ASP.NET workflow, it became possible to see that the application was writing the entire heap of the C runtime library, and the heap bitterly complained about it. And nothing listened to the complaint.

It was necessary to exchange calls to this code in the lock block to ensure that only one thread can call it at a time. This was enough to prevent all failures even under load.

This may still not be enough for all unmanaged code. A piece of code that assumes that all calls will be made in one thread, or that all calls from the same β€œsession” occur in one thread, will be disappointed and can suffer very much or make something worse. In this case, you may need to move this code to a separate Windows service, which allows only one thread to access this library.

+3
source

Source: https://habr.com/ru/post/1344289/


All Articles