简体   繁体   English

在C#中正确放置对象层次结构

[英]Properly disposing an object hierarchy in C#

I am working on a project where a certain class hierarchy implements IDisposable. 我正在一个项目中,某个类的层次结构实现了IDisposable。

Some of the classes also implement a Finalize method ( ~MyClass() ) 一些类还实现了Finalize方法(〜MyClass())

In general it looks something like this: 一般来说,它看起来像这样:

public class BaseClass : IDisposable
{
}

public class SomeClass : BaseClass
{
    ~SomeClass()
    {
        Dispose();
    }

    Dispose()
    {
        // Do some stuff.
        base.Dispose();
    }
}

public class AnoterClass : SomeClass
{
    ~AnoterClass()
    {
        Dispose();
    }

    Dispose()
    {
        // Do some stuff.
        base.Dispose();
    }
}

I'd like to know what is the proper way of handling calls to Dispose, as it seems that these objects are being disposed by calling Dispose(), and later on crash since the Finalize method is called. 我想知道什么是处理对Dispose调用的正确方法,因为似乎这些对象是通过调用Dispose()进行处理的,并且随后由于调用Finalize方法而崩溃。

Is it best to keep a flag at the lowest class in the hierarchy (protected bool disposed), and check that in every level of the class hierarchy? 最好将标志保留在层次结构中的最低类上(放置受保护的布尔值),并在类层次结构的每个级别中进行检查吗?

I find that every possible solution requires some code duplication, which is not what i'm after. 我发现每种可能的解决方案都需要一些代码重复,这不是我要的。

You need to suppress finalization if you are manually disposing of your objects. 如果您要手动处置对象,则需要禁止最终确定。

The pattern to follow is here 遵循的模式在这里

Edit: 编辑:

I think you only need to implement at a particular level of class hierarchy if you have something new to dispose at that level, otherwise I believe that the disposal in the base class will do everything that you need. 我认为,如果您要在该级别上进行某些新设置,则仅需要在该类层次结构的特定级别上实现,否则我相信在基类中进行处理将满足您的所有需求。 If your implementation in any particular class just calls base.Dispose(disposing) then it is not needed, if it has to do some clean uop, then call base.Dispose() then you need it. 如果您在任何特定类中的实现仅调用base.Dispose(disposing)则不需要它,如果它必须执行一些干净的操作,则调用base.Dispose()则需要它。

Having a protected flag as you suggest should be fine. 按照您的建议设置受保护的标志应该没问题。

If your base class implements the standard IDisposable pattern , all you need to do is add the Dispose(bool disposing) override to each derived class that itself owns IDisposable or unmanaged resources: 如果您的基类实现了标准的IDisposable模式 ,则您需要做的就是向每个本身拥有IDisposable或非托管资源的派生类中添加Dispose(bool disposing)重写:

protected override void Dispose(bool disposing)
{
    try
    {
        if (disposing)
        {
            // Release managed resources
        }
        // Release unmanaged resources
    }
    finally
    {
        base.Dispose(disposing);
    }
}

You shouldn't implement finalizers in any of the derived classes. 您不应在任何派生类中实现终结器。

And, of course, any classes in the hierarchy that don't have their own IDisposable resources don't need this override. 而且,当然,层次结构中任何没有自己的IDisposable资源的类都不需要此覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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