0

One of the objects in my application has an object it uses for thread locking:

class Fred
{
   readonly object ThreadLock = new object();

   void Operation()
   {
      lock(ThreadLock)
         DoStuff();
   }
}

I was writing some code to dump the entire contents (all properties and all fields) of an object and ran into something strange. The dump code includes this:

foreach (var field in item.GetType().GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
   var value = field.GetValue(item);
   Debug.WriteLine("Value for field \"" + field.Name + "\" is " + ((value == null) ? "null" : value.ToString()));
}

When it calls GetValue() for the "ThreadLock" field in my "Fred" class, it crashes immediately and repeatably with a 0xC0000005 exception (not a CLR NullReferenceException), and the exception isn't catchable even though I've got code in my main window to (theoretically) catch unhandled exceptions:

AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
   Debug.WriteLine("*** Unhandled exception!");
}

I tried creating a small test class that had the same structure:

public class TryMe
{
   readonly object ThreadLock = new object();

   public void LockTest()
   {
      lock (ThreadLock)
         Test();
   }

   public void Test()
   {
      FieldInfo field = GetType().GetField("ThreadLock", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
      var v = field.GetValue(this);
      Debug.WriteLine("ThreadLock is " + v);
   }
}

var test = new TryMe();
test.Test();
test.LockTest();

And it works correctly every time (no exceptions).

This looks like a bug in CLR, but since I can't reproduce it in a class small enough to post here, I'm not sure how to proceed.

Are there other functions for catching other types of exceptions? Is there something I can query in the FieldInfo class that indicates "hey, don't even try to access this guy"?

Betty Crokker
  • 3,001
  • 6
  • 34
  • 68
  • How does `Operation` invoked in first case? – Pavel Anikhouski Jan 06 '21 at 20:29
  • That class "Fred" is just an indication of what the actual class is doing. The actual class is proprietary (and long) so I can't post it here. – Betty Crokker Jan 06 '21 at 20:31
  • Does it have anything to do with `FlattenHierarchy` => _"Specifies that public and protected **STATIC** members up the hierarchy should be returned. Private static members in inherited classes are not returned. Static members include fields, methods, events, and properties. Nested types are not returned."_ With `field.GetValue(this);` You're trying to return a static value via the instance. (wild guess) – Jeroen van Langen Jan 06 '21 at 20:32
  • Does this answer your question? [Exceptions that can't be caught by try-catch block in application code](https://stackoverflow.com/questions/13564872/exceptions-that-cant-be-caught-by-try-catch-block-in-application-code) It's probably a bug – Charlieface Jan 06 '21 at 20:44
  • Also see https://stackoverflow.com/a/4759831/14868997 – Charlieface Jan 06 '21 at 20:46
  • @JeroenvanLangen Good catch, I'll try removing the FlattenHierarchy and see what happens. But the field in the real class (like the field in the Fred class) isn't static. – Betty Crokker Jan 06 '21 at 20:51
  • There are some reasons for Status Access Violation listed in this post https://stackoverflow.com/a/59035300/1679220 I tried to reproduce the error using multiple threads and scenarios but was unable. – hijinxbassist Jan 06 '21 at 21:15

0 Answers0