IDisposable接口和析构函数

IDisposable接口和析构函数

其实这是一个老话题了,只是刚才做程序的时候突然发现自己有些概念有点模糊,所以做了一个测试如下:

这段代码是MSDN中的例子修改而来。

None.gif using  System;
None.gif
using  System.ComponentModel;
None.gif
None.gif
//  The following example demonstrates how to create
None.gif
//  a resource class that implements the IDisposable interface
None.gif
//  and the IDisposable.Dispose method.
None.gif

None.gif
public   class  DisposeExample
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
public class MyDisposableMember: IDisposable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private bool disposed = false;
InBlock.gif        
private string info;
InBlock.gif
InBlock.gif        
public MyDisposableMember(string _info)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            info 
= _info;
InBlock.gif            System.Diagnostics.Debug.WriteLine(
"\tMyDisposableMember:" + info);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Diagnostics.Debug.WriteLine(
"\tDispose:" + info);
InBlock.gif            Dispose(
true);
InBlock.gif            GC.SuppressFinalize(
this);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!this.disposed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
ExpandedSubBlockEnd.gif                }

InBlock.gif             
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            disposed 
= true;         
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
~MyDisposableMember()      
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Diagnostics.Debug.WriteLine(
"\t~MyDisposableMember():" + info);
InBlock.gif            Dispose(
false);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif    
// A base class that implements IDisposable.
InBlock.gif    
// By implementing IDisposable, you are announcing that 
InBlock.gif    
// instances of this type allocate scarce resources.
InBlock.gif
    public class MyResource: IDisposable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Track whether Dispose has been called.
InBlock.gif
        private bool disposed = false;
InBlock.gif        
private string info;
InBlock.gif        MyDisposableMember member;
InBlock.gif
InBlock.gif        
// The class constructor.
InBlock.gif
        public MyResource(string _info)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            info 
= _info;
InBlock.gif            System.Diagnostics.Debug.WriteLine(
"MyResource:" + info);
InBlock.gif            member 
= new MyDisposableMember("member-" + _info);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Implement IDisposable.
InBlock.gif        
// Do not make this method virtual.
InBlock.gif        
// A derived class should not be able to override this method.
InBlock.gif
        public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Diagnostics.Debug.WriteLine(
"Dispose:" + info);
InBlock.gif            Dispose(
true);
InBlock.gif            
// This object will be cleaned up by the Dispose method.
InBlock.gif            
// Therefore, you should call GC.SupressFinalize to
InBlock.gif            
// take this object off the finalization queue 
InBlock.gif            
// and prevent finalization code for this object
InBlock.gif            
// from executing a second time.
InBlock.gif
            GC.SuppressFinalize(this);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Dispose(bool disposing) executes in two distinct scenarios.
InBlock.gif        
// If disposing equals true, the method has been called directly
InBlock.gif        
// or indirectly by a user's code. Managed and unmanaged resources
InBlock.gif        
// can be disposed.
InBlock.gif        
// If disposing equals false, the method has been called by the 
InBlock.gif        
// runtime from inside the finalizer and you should not reference 
InBlock.gif        
// other objects. Only unmanaged resources can be disposed.
InBlock.gif
        private void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!this.disposed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// If disposing equals true, dispose all managed 
InBlock.gif                
// and unmanaged resources.
InBlock.gif
                if(disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// Dispose managed resources.
InBlock.gif
                    member.Dispose();
ExpandedSubBlockEnd.gif                }

InBlock.gif             
InBlock.gif                
// Call the appropriate methods to clean up 
InBlock.gif                
// unmanaged resources here.
InBlock.gif                
// If disposing is false, 
InBlock.gif                
// only the following code is executed.
InBlock.gif

InBlock.gif                member 
= null;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            disposed 
= true;         
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Use C# destructor syntax for finalization code.
InBlock.gif        
// This destructor will run only if the Dispose method 
InBlock.gif        
// does not get called.
InBlock.gif        
// It gives your base class the opportunity to finalize.
InBlock.gif        
// Do not provide destructors in types derived from this class.
InBlock.gif
        ~MyResource()      
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Do not re-create Dispose clean-up code here.
InBlock.gif            
// Calling Dispose(false) is optimal in terms of
InBlock.gif            
// readability and maintainability.
InBlock.gif
            System.Diagnostics.Debug.WriteLine("~MyResource():" + info);
InBlock.gif            Dispose(
false);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Insert code here to create
InBlock.gif        
// and use the MyResource object.  
InBlock.gif
        MyResource mr = new MyResource("mr");
InBlock.gif        mr.Dispose();
InBlock.gif
InBlock.gif        
new MyResource("hang");
InBlock.gif
InBlock.gif        
using(MyResource m = new MyResource("using"))
InBlock.gif            System.Diagnostics.Debug.WriteLine(
"ready to exit using statement");
InBlock.gif
InBlock.gif        System.GC.SuppressFinalize(mr);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif    运行结果如下:
InBlock.gif    
InBlock.gifMyResource:mr
InBlock.gif    MyDisposableMember:member-mr
InBlock.gifDispose:mr
InBlock.gif    Dispose:member-mr
InBlock.gifMyResource:hang
InBlock.gif    MyDisposableMember:member-hang
InBlock.gifMyResource:using
InBlock.gif    MyDisposableMember:member-using
InBlock.gifready to exit using statement
InBlock.gifDispose:using
InBlock.gif    Dispose:member-using
InBlock.gif    ~MyDisposableMember():member-hang
InBlock.gif~MyResource():hang
InBlock.gif
ExpandedSubBlockEnd.gif     
*/

ExpandedBlockEnd.gif}

None.gif


从运行结果我们可以分析出如下的结果:

1。如果调用Dispose进行释放,则系统不会调用其析构函数
2。如果是系统自动释放的,则不会调用其Dispose函数,也就是说,.net垃圾收集器其实不认识 IDisposable接口。
3。在类析构过程中,会先释放其中的成员,最后再调用类的析构函数本身。

这里第3点需要说明一下,这是和C++中区别很大的地方,在C++中所有的释放都需要类的析构函数来进行,也就是在C++的析构函数中,所有的成员都是可用而且有效的。但是在C#中就会有区别了,因为C#的类析构函数调用之前,系统就会进行托管成员对象的清理工作。从生命周期的观点来看,类成员的生命周期必须包含在类的生命周期之内,C#这么做没有问题,但是问题时在于程序经常需要在析构函数中使用类的成员,所以就出现了异常情况:在析构函数中使用的类成员已经被析构了!也就是说,同样在析构函数范围内,C++包含的寓意是“析构=无效”,但是在C#的析构函数范围中,“析构 != 无效”。

由此,我们明白了为什么一些代码检查的软件会建议我们实现IDisposable时候,象例子中那样要自定义一个Dispose(bool disposing)了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值