Impossible NullReferenceException?
c#, mscorlib, nullreferenceexception
Solution
I'll lay out my hypothesis here.
The stack is leading you to believe that this is where the crash occurs, but it occurs elsewhere. We're looking at the wrong thread.
I don't know if this would be practical, but sometimes good old "printf debugging" helps. What if you print out the value you're looking for before calling `TryGetValue`? You would see whether you strike a null or not.
Problem
I'm investigating an exception that a colleague just got while running an application through Visual Studio 2010: ``` System.NullReferenceException was unhandled by user code Message=Object reference not set to an instance of an object. Source=mscorlib StackTrace: at System.Collections.Generic.GenericEqualityComparer`1.Equals(T x, T y) at System.Collections.Concurrent.ConcurrentDictionary`2.TryGetValue(TKey key, TValue& value) at xxxxxxx.xxxxxxx.xxxxxxx.RepositoryBase`2.GetFromCache(TIdentity id) ``` Using .NET Reflector, I have looked at the code for `GenericEqualityComparer<T>.Equals(T x, T y)`, and I can't see any possible cause for a `NullReferenceException`. ``` //GenericEqualityComparer<T>.Equals(T x, T y) from mscorlib 4.0.30319.269 public override bool Equals(T x, T y) { if (x != null) { return ((y != null) && x.Equals(y)); } if (y != null) { return false; } return true; } ``` The type of `T,` `TKey` and `TIdentity` are all the same type in this stack trace. The type is a custom type called `Identity` that implements `IEquatable<Identity>`. It is immutable and cannot be constructed with null values for the fields that it uses in its implementation of `Equals(Identity other)`. It also overrides `Equals(object obj)` like this: ``` public override bool Equals(object obj) { if ((object)this == obj) { return true; } return Equals(obj as Identity); } public bool Equals(Identity other) { if ((object)this == (object)other) { return true; } if ((object)other == null) { return false; } if (!FieldA.Equals(other.FieldA)) { return false; } return FieldB.Equals(other.FieldB); } ``` I have a fairly exhaustive set of unit tests around the `Equals` implementations. So, it will happily accept a value of null for other/obj and return false as expected. The type does not either override the `==` operators nor `!=` operators. Even so, I would expect to see my class on top of the stack trace if the exception was being thrown from the implementation of `Equals(Identity other)` in my `Identity` class, but it says the `NullReferenceException` is coming from `mscorlib`. I'm running on .NET Framework version 4.0.30319.269. I don't have a memory dump, and I have not seen this before and have not reproduced it since. Still, I'm obliged to investigate and to be absolutely certain that it is not being caused by our code and that it won't happen in production. So, the real question is: What caused this exception? - Bug in mscorlib (seems highly unlikely) - Transient memory corruption on the machine (possible, hard to back up with evidence) - Other? * Updates in response to Jordão * Is it possible to call the method with an object that is not an Identity? The `ConcurrentDictionary<TKey, TValue>` is typed such that `TKey` = `Identity` and nothing subclasses `Identity`. So, I can't see how it could be possible. Is it possible to call the method with null? Unit tests cover the scenario of calling all of the `Equals` implementations with null. What version of the code is the stack trace from? Maybe some older version susceptible to the exception? I'm analyzing the same code that generated the exception. I have checked that the version of the .NET Framework running on my colleagues computer is also 4.0.30319.269. Any multithreaded scenario could cause the exception? These are usually hard to reproduce, but might be worth investigating. Yes, the code is multi-threaded and intended to be. So, that is why I'm using a `ConcurrentDictionary`. * Followup related to response from Jalal Aldeen Saa'd * I would have thought that a race condition where some other thread sets `x` to `null` could only be the cause if the parameter `x` was passed by reference using the 'ref' keyword. I set out to validate that theory with the following code: ``` ManualResetEvent TestForNull = new ManualResetEvent(false); ManualResetEvent SetToNull = new ManualResetEvent(false); [TestMethod] public void Test() { var x = new object(); var y = new object(); var t = Task.Factory.StartNew(() => { return Equals(x, y); }); TestForNull.WaitOne(); //wait until x has been tested for null value x = null; SetToNull.Set(); //signal that x has now been set to null var result = t.Result; Assert.IsFalse(result); } public bool Equals<T>(T x, T y) { if (x != null) { TestForNull.Set(); //signal that we have determined that x was not null SetToNull.WaitOne(); //wait for original x value to be set to null //would fail here if setting the outer scope x to null affected //the value of x in this scope return ((y != null) && x.Equals(y)); } if (y != null) { return false; } return true; } ``` and the test completes without errors. I can force that behavior if I change the signature to pass `x` and `y` by reference (that is, `public bool Equals<T>(ref T x, ref T y) then the test fails with a`NullReferenceException`, but this does not match the method signature of`GenericEqualityComparer.Equals(T x, T y)`.