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.

Haar Feature Face Detection in C#

cp

A new article has been published in CodeProject! This article details the Viola-Jones face detection algorithm available in the Accord.NET Framework. The article page also provides a standalone package for face detection which can be reused without instantiating the entire framework.

 

The article is also participating in CodeProject’s monthly competition under the categories Best C# Article and Best Overall Article of the month! Feel free to cast your vote if you liked and otherwise found them useful!  
This month we have other great articles participating in the competition: Marcelo de Oliveira‘s Social News excels in the Web category; Roy‘s Inline MSIL in C# also presents a very interesting reading in the C# category. The later may eventually be extremely useful to leverage performance in managed applications. 
For those who don’t know, CodeProject is a amazingly useful site which publishes user-created articles and news. Every month, the best articles among all submissions are selected to win prizes and gain popularity as well!