为什么要叫燮pressFinalize当我们没有析构函数 [英] Why should we call SuppressFinalize when we dont have a destructor

查看:123
本文介绍了为什么要叫燮pressFinalize当我们没有析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个问题要问,我是不是能够得到正确的答案。

1)为什么要叫燮pressFinalize在处置功能时,我们没有析构函数。

2)配置,并最终确定用于释放资源之前,该对象被垃圾收集。无论是托管还是非托管资源,我们需要释放它,那么为什么我们需要处置函数中的一个条件,说通'真'时,我们称之为自IDisposable此重写的功能:处置,并通过假从一个finalize调用时

请参阅下面的code,我从网上复制的。

 类测试:IDisposable的
   {
     私人布尔isDisposed = FALSE;

     〜测试()
     {
       处置(假);
     }

     保护无效的Dispose(BOOL处置)
     {
       如果(处置)
       {
         // code处置类的管理资源
       }
       // code处置类的非托管资源

       isDisposed = TRUE;
     }

     公共无效的Dispose()
     {
       处置(真);
       GC.Sup pressFinalize(本);
     }
   }
 

如果我删除布尔保护的处置功能,实施如下。

 类测试:IDisposable的
   {
     私人布尔isDisposed = FALSE;

     〜测试()
     {
       的Dispose();
     }


     公共无效的Dispose()
     {
      // code处置类的管理资源
      // code处置类的非托管资源
      isDisposed = TRUE;

      //调用这一点,因为我们有一个析构函数。如果,如果我们没有一个
       GC.Sup pressFinalize(本);
     }
   }
 

解决方案

我要出去的肢体在这里,但是......大多数人的的所需要的全面的Dispose模式。它的设计是直接访问非托管资源的脸上固体(通常是通过的IntPtr ),并在继承的脸。大部分时间,是实际需要没有这些

如果你只是抱着一个参考,它实现别的东西的IDisposable ,你几乎肯定不需要终结器 - 无论直接持有的资源是负责处理接着就,随即。你可以做这样的事情:

 公共密封类Foo:IDisposable的
{
    私人BOOL处置;
    私人的FileStream流;

    //其他code

    公共无效的Dispose()
    {
        如果(处置)
        {
            返回;
        }
        stream.Dispose();
        处置= TRUE;
    }
}
 

请注意,这的不是的是线程安全的,但是这可能不会是一个问题。

由于不必担心直接控股的子类资源的可能性,你不需要晚饭preSS终结(因为没有一个) - 你并不需要提供的一种方式子类或者自定义处理。生命是没有继承简单。

如果您的的需要允许不受控制的继承(即你不是愿意打赌,子类将有非常特殊的需求),那么你需要去的整个模式。

请注意,与的SafeHandle 从.NET 2.0,重要的是你需要有自己的终结比它在.NET 1.1的更为罕见。


要解决您为什么有一个处理标记在第一时间点:如果你是一个终结之内运行,你参考其他对象可能已经敲定。你应该让他们清理自己,你应该只清理你的的资源直接的自己。

I have few Question for which I am not able to get a proper answer .

1) Why should we call SuppressFinalize in the Dispose function when we don't have a destructor .

2) Dispose and finalize are used for freeing resources before the object is garbage collected. Whether it is managed or unmanaged resource we need to free it , then why we need a condition inside the dispose function , saying pass 'true' when we call this overridden function from IDisposable:Dispose and pass false when called from a finalize.

See the below code i copied from net.

class Test : IDisposable
   {
     private bool isDisposed = false;

     ~Test()
     {
       Dispose(false);
     }

     protected void Dispose(bool disposing)
     {
       if (disposing)
       {
         // Code to dispose the managed resources of the class
       }
       // Code to dispose the un-managed resources of the class

       isDisposed = true;
     }

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

what if I remove the boolean protected Dispose function and implement the as below.

   class Test : IDisposable
   {
     private bool isDisposed = false;

     ~Test()
     {
       Dispose();
     }


     public void Dispose()
     {
      // Code to dispose the managed resources of the class
      // Code to dispose the un-managed resources of the class
      isDisposed = true;

      // Call this since we have a destructor . what if , if we don't have one 
       GC.SuppressFinalize(this);
     }
   }       

解决方案

I'm going out on a limb here, but... most people don't need the full-blown dispose pattern. It's designed to be solid in the face of having direct access to unmanaged resources (usually via IntPtr) and in the face of inheritance. Most of the time, neither of these is actually required.

If you're just holding a reference to something else which implements IDisposable, you almost certainly don't need a finalizer - whatever holds the resource directly is responsible for dealing with that. You can make do with something like this:

public sealed class Foo : IDisposable
{
    private bool disposed;
    private FileStream stream;

    // Other code

    public void Dispose()
    {
        if (disposed)
        {
            return;
        }
        stream.Dispose();
        disposed = true;
    }
}

Note that this isn't thread-safe, but that probably won't be a problem.

By not having to worry about the possibility of subclasses holding resources directly, you don't need to suppress the finalizer (because there isn't one) - and you don't need to provide a way of subclasses customising the disposal either. Life is simpler without inheritance.

If you do need to allow uncontrolled inheritance (i.e. you're not willing to bet that subclasses will have very particular needs) then you need to go for the full pattern.

Note that with SafeHandle from .NET 2.0, it's even rarer that you need your own finalizer than it was in .NET 1.1.


To address your point about why there's a disposing flag in the first place: if you're running within a finalizer, other objects you refer to may already have been finalized. You should let them clean up themselves, and you should only clean up the resources you directly own.

这篇关于为什么要叫燮pressFinalize当我们没有析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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