将IDisposable传递到父IDisposable时的结果是什么?

时间:2017-06-23 07:12:01

标签: c# idisposable

昨天,在我们的代码库上运行Visual Studio代码分析后,以下代码突出显示为一个问题:

bsxfun

返回的警告是

  

警告CA2202对象' stringReader'可以不止一次处理   方法'(方法名称)'。为了避免产生   System.ObjectDisposedException你不应该调用Dispose   一次在一个物体上。

在搜索堆栈溢出之后,我已经大致了解如果我要创建一个包含IDisposable成员的自定义类,它应该实现IDisposable本身,并调用该成员的using (var stringReader = new StringReader(someString)) { using (var reader = XmlReader.Create(stringReader)) { // Code } } 方法。

我的两个问题是

  • 在对象X在创建过程中引用dispose()对象Y作为参数的所有情况下,假设对象X将取Y的所有权并从该点开始,调用{{ 1}} 始终会导致调用IDisposable
  • 这是一段旧代码,警告消息中描述的异常从未报告过(据我所知)。如果假设上述点,为什么双X.dispose()块不会导致调用Y.dispose()两次因此抛出异常?

3 个答案:

答案 0 :(得分:2)

  

假设对象X将取Y的所有权并且从那时开始是正确的,调用X.dispose()将始终导致调用Y.dispose()

不,假设没有保存。让我们检查一下这个具体案例:XmlReader.Create(Stream)

在参考源代码中找到相当多的代码之后,我发现Dispose方法调用了Close方法。这很明显。然后注意this piece of code

public override void Close() {
    Close( closeInput );
}

因此,关闭和处置后备流取决于设置closeInput的值,您可以通过XmlReaderSettings.CloseInput设置设置该值。

所以这里的答案是肯定的:你不能确定它是被处置的。你应该总是确保自己是。

答案 1 :(得分:2)

  • 不,您不能假设另一个对象在处置时会调用Dispose()。具有一次性对象作为参考的对象甚至可能不使用一次性资源。
  • 这个警告有点奇怪。看here看一些有关警告的投诉。你应该设计你的课程,这样就可以安全地多次拨打Dispose()

顺便说一句,MSDN说:

  

方法实现包含可能导致在同一对象上多次调用IDisposable.Dispose或Dispose等效项(如某些类型的Close()方法)的代码路径。

因此,Close()方法调用的路径也可以生成此警告,这就是您在案例中看到此警告的原因。

答案 2 :(得分:1)

  

在对象X在创建过程中将IDisposable对象Y的引用作为参数的所有情况下,假设对象X将获得Y的所有权并且从该点开始,调用X.dispose()将是正确的。总是导致调用Y.dispose()

我想不是,我会试着解释原因。

有一种名为IDisposablePattern的模式,看起来像这样:

public class SimpleClass : IDisposable
{
    // managed resources SqlConnection implements IDisposable as well.
    private SqlConnection _connection;
    private bool _disposed;

    // implementing IDisposable
    public void Dispose()
    {
        // Here in original Dispose method we call protected method with parameter true,
        // saying that this object is being disposed.
        this.Dispose(true);

        // Then we "tell" garbage collector to suppress finalizer for this object because we are releasing
        // its memory and doesnt need to be finalized. Calling finalizer(destructor) of a given type is expensive
        // and tweaks like this help us improve performance of the application.
        GC.SuppressFinalize(this);
    }

    // Following the best practices we should create another method in the class 
    // with parameter saying whether or not the object is being disposed.
    // Its really important that this method DOES NOT throw exceptions thus allowing to be called multiple times 
    protected virtual void Dispose(bool disposing)
    {
        // another thing we may add is flag that tells us if object is disposed already
        // and use it here
        if (_disposed) { return; }
        if (_connection != null)
        {
            _connection.Dispose();
            _connection = null;
        }
        _disposed = true;

        // call base Dispose(flag) method if we are using hierarchy.
    }
}

请注意,当您的类使用非托管资源时,可以将其扩展到新级别:

    public class SimpleClass2: IDisposable
{
    // managed resources
    private SqlConnection _connection;
    private bool _disposed;

    // unmanaged resources
    private IntPtr _unmanagedResources;

    // simple method for the demo
    public string GetDate()
    {
        // One good practice that .NET Framework implies is that when object is being disposed
        // trying to work with its resources should throw ObjectDisposedException so..
        if(_disposed) { throw new ObjectDisposedException(this.GetType().Name);}

        if (_connection == null)
        {
            _connection = new SqlConnection("Server=.\\SQLEXPRESS;Database=master;Integrated Security=SSPI;App=IDisposablePattern");
            _connection.Open();
        }
        // allocation of unmanaged resources for the sake of demo.
        if (_unmanagedResources == IntPtr.Zero)
        {
            _unmanagedResources = Marshal.AllocHGlobal(100 * 1024 * 1024);
        }

        using (var command = _connection.CreateCommand())
        {
            command.CommandText = "SELECT getdate()";
            return command.ExecuteScalar().ToString();
        }
    }


    public void Dispose()
    {
        // Here in original Dispose method we call protected method with parameter true,
        // saying that this object is being disposed.
        this.Dispose(true);

        // Then we "tell" garbage collector to suppress finalizer for this object because we are releasing
        // its memory and doesnt need to be finalized. Calling finalizer(destructor) of a given type is expensive
        // and tweaks like this help us improve performance of the application.

        // This is only when your class doesnt have unmanaged resources!!!
        // Since this is just made to be a demo I will leave it there, but this contradicts with our defined finalizer.
        GC.SuppressFinalize(this);
    }

    // Following the best practices we should create another method in the class 
    // with parameter saying wether or not the object is being disposed.
    // Its really important that this method DOES NOT throw exceptions thus allowing to be called multiple times 
    protected virtual void Dispose(bool disposing)
    {
        // another thing we may add is flag that tells us if object is disposed already
        // and use it here
        if (_disposed) { return; }
        // Thus Dispose method CAN NOT release UNMANAGED resources such as IntPtr structure,
        // flag is also helping us know whether we are disposing managed or unmanaged resources
        if (disposing)
        {
            if (_connection != null)
            {
                _connection.Dispose();
                _connection = null;
            }
            _disposed = true;
        }
        // Why do we need to do that?
        // If consumer of this class forgets to call its Dispose method ( simply by not using the object in "using" statement
        // Nevertheless garbage collector will fire eventually and it will invoke Dispose method whats the problem with that is if we didn't 
        // have the following code unmanaged resources wouldnt be disposed , because as we know GC cant release unmanaged code.
        // So thats why we need destructor(finalizer).
        if (_unmanagedResources != IntPtr.Zero)
        {
            Marshal.FreeHGlobal(_unmanagedResources);
            _unmanagedResources = IntPtr.Zero;;
        }
        // call base Dispose(flag) method if we are using hierarchy.
    }

    ~DatabaseStateImpr()
    {
        // At this point GC called our finalizer method , meaning 
        // that we don't know what state our managed resources are (collected or not) because
        // our consumer may not used our object properly(not in using statement) so thats why
        // we skip unmanaged resources as they may have been finalized themselves and we cant guarantee that we can
        // access them - Remember? No exceptions in Dispose methods.
        Dispose(false);
    }
}