What's a good way of writing a test to check that the dispose method of the IDisposable
interface is properly releasing unmanaged resources after it is called?
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
// Free any other managed objects here.
}
// Free any unmanaged objects here
theUnmanagedResource.Dispose();
disposed = true;
}
I was thinking I could check if disposed is false after, but it doesn't guarantee that resources are managed.
Another way is setting theUnmanagedResource = null
after theUnmanagedResources.Dispose()
and check if it is null after in the test cases. But from other posts, they state setting disposed resources to null after is not a good : Setting Objects to Null/Nothing after use in .NET