简体   繁体   中英

How to dispose managed and unmanaged objects in C#?

all. I've never worked with destructors nor dispose, so this is new to me. I have a task of doing a class that has destructor and dispose methods, and that has a UInt64 Id property that is auto-incremental, and a static Dictionary<UInt64,MyClass> that must reference by Id all live instances of MyClass .

After searching how to use them properly, this is what I ended up doing:

public class MyClass : IDisposable
{
    private static Object Lock = new Object();
    private static Dictionary<UInt64, MyClass> LiveInstances = new Dictionary<UInt64, MyClass>();

    public UInt64 Id { get; private set; }

    public MyClass()
    {
        lock (Lock)
        {
            var newId = IncrementalId();
            if (newId == 0)
            {
                throw new ArgumentException("Reached MAX VAL");
            }
            Id = newId;
            LiveInstances.Add(Id, this);
        }
    }

    ~MyClass()
    {
       CleanUpNativeResources();
    }     

    public void Dispose()
    {
        lock (Lock)
        {
            CleanUpManagedResources();
            CleanUpNativeResources();
            GC.SuppressFinalize(this);
        }
    }

    protected virtual void CleanUpManagedResources()
    {
        LiveInstances.Remove(Id);

    }
    protected virtual void CleanUpNativeResources()
    {

    }

    private static UInt64 IncrementalId()
    {
        for (ulong i = 0; i <= Convert.ToUInt64(LiveInstances.Count) ; i++)
        {
            if (i != UInt64.MaxValue && !LiveInstances.ContainsKey(i + 1))
            {
                return i+1;
            }
        }
        return 0;
    }
}

Now, my question is How do I dispose objects? Whether I try to find examples of disposing objects, I find something like this:

 // Code to dispose the managed resources of the class
 Console.WriteLine("Object disposed");

Thanks in advance.

Managed resources will be disposed automatically by the garbage collector at some point of time. Unmanaged resources are things like Filehandles, obtained by making a Windows API call that returns a Windows Handle which must be freed manually. You dont have anything which requires manual disposal. If you don't free those handles, they will remain allocated for the duration of the program, but all .Net classes that have an unmanaged resource provide a Finalizer (see below) to make sure that they will normally be freed at some point.

(But if you were writing your own file handling class and forgot to free the file handle anywhere at all, the file would remain open until your program exited.)

Normally such unmanaged resources will be freed in two places:

The Dispose() method. This should be the normal way that you dispose unmanaged resources.

The Finalizer. This is a last-resort mechanism. If a class has a finalizer it will be called by the Garbage Collector when it cleans up a dead object. Any class which has an unmanaged resource should have a finalizer to clean up if the programmer forgets to call Dispose().

The basic Dispose pattern used goes something like this:

class MyObject : IDisposable
{
    //indicates if dispose has already been called
    //private bool _disposed = false;

    //Finalize method for the object, will call Dispose for us
    //to clean up the resources if the user has not called it
    ~MyObject()
    {
        //Indicate that the GC called Dispose, not the user
        Dispose(false);
    }

    //This is the public method, it will HOPEFULLY but
    //not always be called by users of the class
    public void Dispose()
    {
        //indicate this was NOT called by the Garbage collector
        Dispose(true);

        //Now we have disposed of all our resources, the GC does not
        //need to do anything, stop the finalizer being called
        GC.SupressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        //Check to see if we have already disposed the object
        //this is necessary because we should be able to call
        //Dispose multiple times without throwing an error
        if (!disposed)
        {
            if (disposing)
            {
                //clean up managed resources
                components.Dispose();
            }

            //clear up any unmanaged resources - this is safe to
            //put outside the disposing check because if the user
            //called dispose we want to also clean up unmanaged
            //resources, if the GC called Dispose then we only
            //want to clean up managed resources
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM