Search code examples
garbage-collectionidisposablefinalizer

IDisposable pattern: shall I annulate vars when GC.SupressFinalizer


Whenever I call GC.SupressFinalizer() in the Dispose method, should I assign null to all instance members to have them cleaned up, or they would be removed in any case ?

For example:

class test : IDisposable
{ 
    public int a = 10;
    public int b = 20;
    public int c = 39;

    private bool is_disposed = false;

    Dispose(bool shall_dispose)
    {
        if(!is_disposed && shall_dispose)
        {
            is_disposed = true;

            // shall I annulate a, b & c or they will be freed anyway??
            // a = null; ?, integers aint nullable 
            // b = null; ?
            // c = null; ?

            // or primitive types should't be annulated? only reference(heap) types?
        }
    }

    Dispose()
    {
        Dispose(true);
    }

    ~test()
    {
        Dispose(false);
    }
}

Solution

  • GC.SupressFinalizer() will not stop your object from being collected when it is no longer reachable, it will just supress the call to the object's finalizer. So both your object and its members will be collected, no matter wether you assign null or not.