[英]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". 当然 - 你应该让
CoupleFrames
实现IDisposable
, 它的 Dispose
方法应该处理它“拥有”的对象。 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: 需要注意的几点:
CoupleFrame
really "owns" these constituent objects. CoupleFrame
真正“拥有”这些组成对象。 Disposal relies on a clear ownership model CoupleFrame
isn't sealed (and can't be) you may need to go into a more complicated pattern with virtual methods and finalizers. CoupleFrame
没有密封(并且不能),您可能需要使用虚拟方法和终结器进入更复杂的模式。 It can get very complicated, and you should read the advice given here by Joe Duffy et al . CoupleFrame
implement IDisposable
, you're basically telling all clients that they should dispose of any instance they own. CoupleFrame
实现IDisposable
,您基本上告诉所有客户他们应该处置他们拥有的任何实例。 If you're not happy with imposing that burden, you need to rethink the design a bit. I would implement the Dispose pattern 我会实现Dispose模式
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. 在你的类中实现IDisposable并让它从那里处理成员变量。
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. 您可以使用
IDisposable
接口。
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. 您可以使用析构函数来调用Dispose方法,因为GC有时可以删除该对象。
Make CoupleFrames Implement the Idisposable Interface. 使CoupleFrames实现Idisposable接口。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.