Question

We are getting mysterious SEHExceptions in our C# application which may be coming from some unmanaged code (FFMPEG). Its not being caught by the try-catch blocks - so we are not certain what is causing it, but it is perhaps due to a null reference.

I found this on MSDN (SEHException MSDN Page) which says:

The .NET Framework often encounters unmanaged SEH exceptions that are automatically mapped to managed equivalents. There are two common unmanaged SEH exceptions: STATUS_NO_MEMORY exceptions are automatically mapped to the OutOfMemoryException class. STATUS_ACCESS_VIOLATION exceptions are automatically mapped as follows:

If legacyNullReferencePolicy is applied, all access violations are mapped to the NullReferenceException class.

Now, this bit sounds interesting - if we could set this legacyNullReferencePolicy maybe these mysterious NullReferenceExceptions could be caught and we could find out where they are coming from.

But I can't find any information online about legacyNullReferencePolicy. What is it? Where do I set it?

Était-ce utile?

La solution

I guess that legacyNullReferencePolicy is renamed in the final bits of .Net 4.0

You are looking for the legacyCorruptedStateExceptionsPolicy setting in the <runtime> config section like so:

<configuration>
   <runtime>
      <legacyCorruptedStateExceptionsPolicy enabled="true" />
   </runtime>
</configuration>

or decorate your method that needs to handle those state corruption exceptions with this attribute:HandleProcessCorruptedStateExceptionsAttribute:

This example is taken from this msdn article in the CLR Inside Out series of author Andrew Pardoe

[HandleProcessCorruptedStateExceptions] 
[SecurityCritical]
public static int Main() 
{ 
   try
     {
       // Catch any exceptions leaking out of the program 
     }
   catch (Exception e) 
       // We could be catching anything here 
     {

        System.Console.WriteLine(e.Message); 
        return 1; 
     } 

   return 0; 

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top