TomPearson

Hello

I have a class that implements IDisposable with the following code.

/// <summary>

/// This should be called to free up system resources when the object is finished with

/// </summary>

public void Dispose()

{

// Call the clean up routine

CleanUp(true);

// Stop the finializer calling

GC.SuppressFinalize(this);

}

#endregion

/// <summary>

/// Dispose of the managed resources

/// </summary>

/// <param name="disposing"></param>

private void CleanUp(bool disposing)

{

// Check we haven't already been disposed of

if (!m_disposed)

{

if (disposing)

{

// Clean up the managed resources

}

// Always clean up the unmanaged resources

}

// Set the flag to show the object has been disposed

m_disposed = true;

}

/// <summary>

/// Finalizer method called on garbage collection

/// </summary>

~Memento()

{

// Clean up specifying that it is not being disposed rather finalized

CleanUp(false);

}

I have written some unit tests and when checking the coverage I get the Finalize method being called just once when the tests complete. However as far as I can see I dispose of the object every time it is created or used.

I obviously want to dispose every time the object is used but I cannot tell from the call stack what is leaving the object behind as it simple contains the finalize method.

Is there anyway of working out what is happening



Re: Visual C# General Finalize and Dispose

Thomas Danecker

Could you post your test-method

This code looks ok (apart that you should rename your CleanUp to Dispose according to the pattern).






Re: Visual C# General Finalize and Dispose

TomPearson

This object is actually used internally to a package that provides a host of functionality and as such I am testing the package functionality rather than simply the class. This means that the test methods make no sense without the rest of the code - which is multiple files.





Re: Visual C# General Finalize and Dispose

Ernst Kuschke

That code looks fine to me. What does your test look like Why do you think Finalise should be called multiple times





Re: Visual C# General Finalize and Dispose

TomPearson

Sorry I don't think the issue was phrased right. I was wanting to ensure the Finalize was never being called and therefore my problem was that it was.

However I have been through my tests 1 at a time and found out why (exception breaking out of method).

Thanks for the interest shown

Tom