Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a Disposable class

Tags:

c#

I have this 'PlayerClass' class and every time I instantiate it I need to dispose of it, including the fields inside that class. Is there a way to do it? This is my class:

internal class PlayerClass 
{
    public WindowsMediaPlayer _wplayer;
}

How do I get to dispose of the class after using it? I have tried to find a way on the internet but none of them are working after testing it.

I've tried this:

internal class PlayerClass : IDisposable
{
    public WindowsMediaPlayer _wplayer;
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    
    /// <summary>
    /// Is this instance disposed?
    /// </summary>
    protected bool Disposed { get; private set; }
    
    /// <summary>
    /// Dispose worker method. See http://coding.abel.nu/2012/01/disposable
    /// </summary>
    /// <param name="disposing">Are we disposing? 
    /// Otherwise we're finalizing.</param>
    protected virtual void Dispose(bool disposing)
    {
        Disposed = true;
    }
}

What am I doing wrong?

like image 991
bRaNdOn Avatar asked Aug 20 '14 01:08

bRaNdOn


People also ask

What is disposable class in C#?

Dispose implementation is called by the consumer of a type when the resources owned by an instance are no longer needed, you should either wrap the managed object in a SafeHandle (the recommended alternative), or you should override Object.

How do you implement a Dispose method?

Implement the dispose pattern for a derived class Instead, to clean up a derived class, you provide the following: A protected override void Dispose(bool) method that overrides the base class method and performs the actual cleanup of the derived class. This method must also call the base. Dispose(bool) ( MyBase.

What happens if Dispose is not called?

Implement a finalizer to free resources when Dispose is not called. By default, the garbage collector automatically calls an object's finalizer before reclaiming its memory.

Does the garbage collector call Dispose?

The GC does not call Dispose , it calls your finalizer (which you should make call Dispose(false) ).


1 Answers

It looks as if you're implementing IDisposable just for the heck of it which is not necessary for managed code. The garbage collector will automatically clean up behind you.

You may need to clarify in your post why you are implementing IDisposable.

However I believe your mistake is that Dispose() is not automatically called by the garbage collector. If you do implement IDisposable you must make sure that the code using your class either instantiates inside a using() statement or manually calls .Dispose. Otherwise your dispose method will never fire.

using(var player = new PlayerClass()){
    // Do something with player
    // player.Dispose() is called automatically when you exit this using statement.
}

Since you're relying on the caller to make sure Dispose is called you may also want to look into a SafeHandle (preferred) or Finalize.

Because the IDisposable.Dispose implementation is called by the consumer of a type when the resources owned by an instance are no longer needed, you should either wrap the managed object in a SafeHandle (the recommended alternative), or you should override Object.Finalize to free unmanaged resources in the event that the consumer forgets to call Dispose.

Source: IDisposable Interface

Using Statement https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement

like image 156
Timeout Avatar answered Oct 25 '22 19:10

Timeout