Question

Is there any downside of calling GC.SuppressFinalize(object) multiple times?
Protected Dispose(bool) method of the dispose pattern checks whether it is called before but there is no such check in the public Dispose() method.

public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    if (_Disposed)
        return;

    if (disposing)
    {
        // Cleanup managed resources.
    }

    // Cleanup unmanaged resources.
    _Disposed = true;
}

~MyClass() { Dispose(false); }

Is it ok to call the Dispose() method of a MyClass instance multiple times?

Était-ce utile?

La solution

According to docs: http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx, it sets some bit in object header, so there shouldn't be any implications of calling it multiple times.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top