How to remove managed and unmanaged objects in C #?

everything. I have never worked with destructors or disposed of, so this is new to me. I have the task of making a class that has destructor and deletion methods, and has an UInt64Id property that is automatically incremental, and a static property Dictionary<UInt64,MyClass>that should reference Id all live instances MyClass.

After searching how to use them correctly, this is what I ended up with:

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 place objects? I try to find examples of deleting objects, I find something like this:

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

Thanks in advance.

+4
source share
1

. - , Filehandles, Windows API, Windows, . , .   , , .Net, , Finalizer (. ), , .

( , .)

:

Dispose(). , .

. . , , . , , , Dispose().

Dispose :

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
        }
    }
}
+3

Source: https://habr.com/ru/post/1616559/


All Articles