idisposable.dispose()是自动调用的吗?

发布于 2025-02-06 04:24:03 字数 567 浏览 2 评论 0原文

可能的重复:
垃圾收集器会呼叫iDisposable.dispose。 >

我有一个具有一些不受管理的资源的课程。我的班级实现了idisposable接口,并在dispose()方法中释放了未管理的资源。我是否必须调用dispose()方法,还是以某种方式自动称为?垃圾收集器会称呼吗?

Possible Duplicate:
Will the Garbage Collector call IDisposable.Dispose for me?

I have a Class which has some unmanaged resources. My class implements the IDisposable interface and releases the unmanaged resources in the Dispose() method. Do I have to call the Dispose() method or will it be automatically called somehow? Will the Garbage Collector call it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我家小可爱 2025-02-13 04:24:04

dispose()将不会自动调用。如果有终结器将自动调用。实施idisposable为您的类用户提供了一种方法,可以尽早发布资源,而不是等待垃圾收集器。

客户端的最佳方法是使用使用语句使用语句,该语句即使有例外,也可以处理dispose()的自动调用。

iDisposable的正确实现是:

class MyClass : IDisposable
{
  private bool disposed = false;

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

  protected virtual void Dispose(bool disposing)
  {
    if(!disposed)
    {
      if(disposing)
      {
        // Manual release of managed resources.
      }
      // Release unmanaged resources.
      disposed = true;
    }
  }

  ~MyClass() { Dispose(false); }
}

如果类调用的用户dispose()清理直接进行。如果对象被垃圾收集器捕获,则调用Dispose(false)进行清理。请注意,当从最终器中调用(〜MyClass方法)托管引用可能无效,因此只能发布非管理资源。

Dispose() will not be called automatically. If there is a finalizer it will be called automatically. Implementing IDisposable provides a way for users of your class to release resources early, instead of waiting for the garbage collector.

The preferable way for a client is to use the using statement which handles automatic calling of Dispose() even if there are exceptions.

A proper implementation of IDisposable is:

class MyClass : IDisposable
{
  private bool disposed = false;

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

  protected virtual void Dispose(bool disposing)
  {
    if(!disposed)
    {
      if(disposing)
      {
        // Manual release of managed resources.
      }
      // Release unmanaged resources.
      disposed = true;
    }
  }

  ~MyClass() { Dispose(false); }
}

If the user of the class calls Dispose() the cleanup takes place directly. If the object is catched by the garbage collector, it calls Dispose(false) to do the cleanup. Please note that when called from the finalizer (the ~MyClass method) managed references may be invalid, so only unmanaged resources can be released.

假装不在乎 2025-02-13 04:24:04

如果您在语句中在中实例化对象,则当代码退出使用 block时,请拨打(),

using(var myObject = new MyDisposableObject())
{
  blah();
} // Dispose() is called here (or whenever the code exits the block)

如果您不使用使用使用,然后取决于您(调用代码)通过明确调用dispose()来处理对象。

另外,如果您的呼叫者不调用Dispose(),则您(MyObject的实现者)可以添加对最终化器的支持。更多信息在这里

If you instantiate your object in a using statement, Dispose() is called for you when code exits the using block

using(var myObject = new MyDisposableObject())
{
  blah();
} // Dispose() is called here (or whenever the code exits the block)

If you don't use using, then it's up to you (the calling code) to dispose of your object by explicitely calling Dispose().

Also, you (the implementor of MyObject) can add support for a finalizer in case your caller doesn't call Dispose(). More info here.

瑾兮 2025-02-13 04:24:04

构造中

using(var myclass = new MyClass())
{
   // do something with myclass
}

// now 'myclass'is Disposed

将必须手动调用此方法,也许(假设“ myClass”实现“ Idisposable”)在 c#8.0 中的

using var myclass=new MyClass();

,您可以按以下语句写入:并且“ myclass”将在范围末尾自动处置。

使用语句的优点(与调用Dispose()显式相比)是dispose()无论您如何退出此块:通过经过末端,遇到返回语句或抛出异常。

You will have to call this method manually, maybe (assuming that "MyClass" implements "IDisposable") in a construct like

using(var myclass = new MyClass())
{
   // do something with myclass
}

// now 'myclass'is Disposed

In C# 8.0, you can write as below statement:

using var myclass=new MyClass();

and the "myclass" will be disposed automatically at the end of the scope.

The advantage of the using statement (compared to calling Dispose() explicitly) is that Dispose() is called no matter how you exit this block: by running past the end, encountering a return statement or having an exception thrown.

肤浅与狂妄 2025-02-13 04:24:04

为了确保正确处理资源,您需要实现iDisposable和调用在Destructor(finalizer)中处置

class Foo : IDisposable
{
    private bool m_disposed = false;

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

    ~Foo()
    {
        Dispose(false);
    }

    protected void Dispose(bool disposing)
    {
        if (!m_disposed)
        {
            if (disposing)
            { 
                //release managed resources
            }
            //release unmanaged resources

            m_disposed = true;
        }
    }
}

To make sure the resources are correctly disposed, you need to both implement IDisposable and call Dispose in the destructor(finalizer).

class Foo : IDisposable
{
    private bool m_disposed = false;

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

    ~Foo()
    {
        Dispose(false);
    }

    protected void Dispose(bool disposing)
    {
        if (!m_disposed)
        {
            if (disposing)
            { 
                //release managed resources
            }
            //release unmanaged resources

            m_disposed = true;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文