多次调用SuppressFinalize [英] Calling SuppressFinalize multiple times

查看:97
本文介绍了多次调用SuppressFinalize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GC.SuppressFinalize(object)多次是否存在任何缺点?

受保护 Dispose(bool) 处理模式的方法检查它是否在之前被调用,但是在public Dispose()方法中没有这种检查。

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

保护虚拟无效Dispose(bool处置)
{
if(_Disposed)
return;

if(处置)
{
//清理管理资源。
}

//清理非托管资源。
_Disposed = true;
}

〜MyClass(){Dispose(false); }

可以调用 Dispose()一个 MyClass 实例多次的方法?

解决方案

docs: http://msdn.microsoft.com/en-us /library/system.gc.suppressfinalize.aspx ,它在对象标题中设置了一些位,所以不应该有多次调用它的任何暗示。


Is there any downside of calling GC.SuppressFinalize(object) multiple times?
Protected Dispose(bool) method of the dispose pattern checks whether it is called before but there is no such check in the public Dispose() method.

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

protected virtual void Dispose(bool disposing)
{
    if (_Disposed)
        return;

    if (disposing)
    {
        // Cleanup managed resources.
    }

    // Cleanup unmanaged resources.
    _Disposed = true;
}

~MyClass() { Dispose(false); }

Is it ok to call the Dispose() method of a MyClass instance multiple times?

解决方案

According to docs: http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx, it sets some bit in object header, so there shouldn't be any implications of calling it multiple times.

这篇关于多次调用SuppressFinalize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆