In our framework, there is some key objects which have file handles or WCF client connections. Those objects are IDiposable
and we have validation code (with exceptions being thrown) to ensure that they are getting properly disposed when not needed anymore. (Debug-only so that we don't want to crash on release). This is not necessarily on shutdown.
On top of this, we have unit tests which run our code and we thus expect them to fail if we forget such disposals.
Problem is: On .NET 4.5.1, with NUnit (2.6.3.13283) runner (or with ReSharper, or TeamCity) does not trigger test failure when such exception in the Finalizer
are thrown.
Weird thing is: Using NCrunch (with is over NUnit also), unit tests DO fail! (Which locally for me, at least I can find such missing disposals)
That's pretty bad, since our build machine (TeamCity) does not see such failures and we think that everything is good! But running our software (in debug) will indeed crash, showing that we forgot a disposal
Here's an example that shows that NUnit does not fail
public class ExceptionInFinalizerObject
{
~ExceptionInFinalizerObject()
{
//Tried here both "Assert.Fail" and throwing an exception to be sure
Assert.Fail();
throw new Exception();
}
}
[TestFixture]
public class FinalizerTestFixture
{
[Test]
public void FinalizerTest()
{
CreateFinalizerObject();
GC.Collect();
GC.WaitForPendingFinalizers();
}
public void CreateFinalizerObject()
{
//Create the object in another function to put it out of scope and make it available for garbage collection
new ExceptionInFinalizerObject();
}
}
Running this in the NUnit runner: everything is green. Asking ReSharper to debug this test will indeed step into the Finalizer.