简体   繁体   中英

Disposing object in C#

I have written the following class:

public class CoupleFrames
{
    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public ColorImageFrame colorFrame;
    public Bitmap desktopFrame;
}

Now I'm using the following code for disposing the variables.

CoupleFrames cf = new CoupleFrames(frame1, frame2);
// some code...
cf.colorFrame.Dispose();
cf.desktopFrame.Dispose();

I'm not sure that this is the correct way. Someone can suggest me the correct way for disposing the entire object?

I'm not sure that this is the correct way. Someone can suggest me the correct way for disposing the entire object?

Sure - you should make CoupleFrames implement IDisposable , and its Dispose method should dispose of the objects it "owns". For example:

public sealed class CoupleFrames : IDisposable
{
    private readonly ColorImageFrame colorFrame;
    private readonly Bitmap desktopFrame;

    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        // TODO: Argument validation, unless it's valid for these parameters
        // to be null, in which case the Dispose method would need to be careful.
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public void Dispose()
    {
        colorFrame.Dispose();
        desktopFrame.Dispose();
    }
}

A few points to note:

  • You should make sure it's clear that the CoupleFrame really "owns" these constituent objects. Disposal relies on a clear ownership model
  • If CoupleFrame isn't sealed (and can't be) you may need to go into a more complicated pattern with virtual methods and finalizers. It can get very complicated, and you should read the advice given here by Joe Duffy et al . If your class is sealed, a lot of that complexity goes away
  • Public fields are generally a bad idea (in terms of encapsulation), which is why I've made them private here. I've also made them readonly, as if they can be changed later you need to think about whether changing them should dispose of the previously-referenced object etc.
  • By making CoupleFrame implement IDisposable , you're basically telling all clients that they should dispose of any instance they own. If you're not happy with imposing that burden, you need to rethink the design a bit.

I would implement the Dispose pattern

public class CoupleFrames : IDisposable
{
    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public ColorImageFrame colorFrame;
    public Bitmap desktopFrame;

    private bool disposed;

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

    protected virtual void Dispose(bool disposing)
    {
        if (disposed)
        {
            return;
        }
        if (disposing)
        {
            colorFrame.Dispose();
            desktopFrame.Dispose();
        }
        disposed = true;
    }
}

Implement IDisposable in your class and have it dispose of the member variables from there.

public class CoupleFrames : IDisposable
        {
            public CoupleFrames(ColorImageFrame cif, Bitmap df)
            {
                this.colorFrame = cif;
                this.desktopFrame = df;
            }

            public ColorImageFrame colorFrame;
            public Bitmap desktopFrame;

            public void Dispose()
            {
                this.colorFrame.Dispose();
                this.desktopFrame.Dispose();
            }
        }

You can use the IDisposable interface.

public class CoupleFrames : IDisposable
{
    ....

    public void Dispose()
    {
        // Your disposing code here
    }

    ~CoupleFrames()
    {
        Dispose();
    }
}

You can use the destructor to call the Dispose method since the object can sometimes be deleted by the GC.

Make CoupleFrames Implement the Idisposable Interface.

public class CoupleFrames : IDisposable
{
    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public ColorImageFrame colorFrame;
    public Bitmap desktopFrame;

    public void Dispose()
    {
        cf.colorFrame.Dispose();
        cf.desktopFrame.Dispose();
    }

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