首页 新闻 会员 周边 捐助

.NET中非托管资源问题

0
悬赏园豆:10 [已解决问题] 解决于 2009-08-27 16:07

过去我以为实现了IDisposable接口的类一定是非托管资源,今天看了MSDN一段代码:

 

public class BaseResource : IDisposable
    {
        // Pointer to an external unmanaged resource.
        private IntPtr handle;
        // Other managed resource this class uses.
        private Component Components;
        // Track whether Dispose has been called.
        private bool disposed = false;

        // Constructor for the BaseResource object.
        public BaseResource()
        {
            // Insert appropriate constructor code here.
        }

        // Implement IDisposable.
        // Do not make this method virtual.
        // A derived class should not be able to override this method.
        public void Dispose()
        {
            Dispose(true);
            // Take yourself off the Finalization queue
            // to prevent finalization code for this object
            // from executing a second time.
            GC.SuppressFinalize(this);
        }

        // Dispose(bool disposing) executes in two distinct scenarios.
        // If disposing equals true, the method has been called directly
        // or indirectly by a user's code. Managed and unmanaged resources
        // can be disposed.
        // If disposing equals false, the method has been called by the
        // runtime from inside the finalizer and you should not reference
        // other objects. Only unmanaged resources can be disposed.
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    // Dispose managed resources.
                    Components.Dispose();
                }
                // Release unmanaged resources. If disposing is false,
                // only the following code is executed.
                CloseHandle(handle);
                handle = IntPtr.Zero;
                // Note that this is not thread safe.
                // Another thread could start disposing the object
                // after the managed resources are disposed,
                // but before the disposed flag is set to true.
                // If thread safety is necessary, it must be
                // implemented by the client.

            }
            disposed = true;
        }

        // Use C# destructor syntax for finalization code.
        // This destructor will run only if the Dispose method
        // does not get called.
        // It gives your base class the opportunity to finalize.
        // Do not provide destructors in types derived from this class.
        ~BaseResource()
        {
            // Do not re-create Dispose clean-up code here.
            // Calling Dispose(false) is optimal in terms of
            // readability and maintainability.
            Dispose(false);
        }

        // Allow your Dispose method to be called multiple times,
        // but throw an exception if the object has been disposed.
        // Whenever you do something with this class,
        // check to see if it has been disposed.
        public void DoSomething()
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException();
            }
        }
    }

其中Component继承自IDisposable,注释它是托管资源,是不是可以这样理解,Component可能存在非托管资源,但是只要它实现了IDisposable就可以认为非托管资源的释放有垃圾收集器来保证,所以他变成了托管资源。不知道我可说清楚了,我现在想问,我理解的是否正确?

 

吴畏的主页 吴畏 | 菜鸟二级 | 园豆:426
提问于:2009-08-25 10:24
< >
分享
最佳答案
0

Some of the most commonly used components in .NET Framework programming are the visual controls that you add to Windows Forms such as the Button Control (Windows Forms), ComboBox Control (Windows Forms), and so on. Non-visual components include the Timer Control, SerialPort, and ServiceController among others.

 

一些组件 比如 SerialPort 是需要访问非托管资源的,所有需要实现 IDisposable 接口

收获园豆:5
eaglet | 专家六级 |园豆:17139 | 2009-08-25 11:25
// Other managed resource this class uses. private Component Components; MSDN为什么注释这个字段为托管资源,请指教
吴畏 | 园豆:426 (菜鸟二级) | 2009-08-25 11:37
Component 本身是托管的,只是某些Component 需要访问非托管资源而已。 就像 System.IO 下很多类都是托管的,但需要访问非托管资源。 MSDN 注释这么写是为了和上面那个 private IntPtr handle; 非托管资源做区别。
eaglet | 园豆:17139 (专家六级) | 2009-08-25 14:18
其他回答(1)
0

没有人规定IDisposable一定是非托管资源,只是说在该接口的Dispose方法中要注意释放非托管资源,但是如果对象本身不持有任何的非托管资源也是可以的,甚至是必须的:

public class ConnectionHelper : IDisposable {

  private DbConnection m_Connection;

  public void Dispose() {

    m_Connection.Dispose();

  }

}

很明显,DbConnection是非托管的,但是你必须在Dispose方法中区释放m_Connection对象

收获园豆:5
Gray Zhang | 园豆:17610 (专家六级) | 2009-08-26 13:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册