The Mistery of Failing Tests – Or how to implement IDisposable correctly

vs-test-error

Some days ago my Visual Studio tests started failing randomly. Curiously, the same tests which failed in a previous run would succeed if ran again. There seemed to be nothing wrong with the tests, except for the message “The agent process was stopped while the test was running” appearing the Test Results window.

It turns out this message tends to appear whenever an object throws an exception inside its destructor.

Luckly, only about a dozen classes in Accord.NET do need to implement finalizers. After a few searches, it turned down the symptoms were being caused by a NullReferenceException being thrown in one of those classes.

The solution is to always implement the Disposable pattern correctly.

Disposing managed code correctly

To do so, first, implement IDisposable. Consider, for example, the Metronome class, which makes use of timers:


Then, implement the required Dispose method, but do not write the dispose code right away. Instead, write:


Always check if a member is not null before disposing it. Alternatively, after disposing, always set it to null so you can’t accidentaly dispose it twice. The GC.SuppressFinalize instructs the Garbage Collector that the object has already been disposed, so it won’t be required to call its finalizer. The GC will be trusting you have already cleaned the room; so don’t fail on it!

Finally, write the finalizer as:

Disposing unmanaged code

The disposal of unmanaged code follows exactly the same pattern as above, but some care is required when disposing unmanaged resources. Consider the Signal class, which represents an audio signal in PCM format:

Note that this class uses an unmanaged memory block to store the audio signal. To dispose it properly, the Disposable pattern has to be implemented as it follows:


Here we have almost the same pattern. However, note that unmanaged code should always be freed, even if Dispose has not been called explictly. So the code for releasing unmanaged resources is executed unconditionally in the Dispose(bool) method.

Leave a Reply

Your email address will not be published. Required fields are marked *