有关Dispose,Finalize,GC.SupressFinalize函数-托管与非托管资源释放的模式
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                有关Dispose,Finalize,GC.SupressFinalize函数-托管与非托管资源释放的模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            //這段代碼來自官方示例,刪除了其中用處不大的細節
using System; using System.ComponentModel;/**** 這個模式搞的這么復雜,目的是:不管使用者有沒有手動調用Dipose函數,都能保證托管資源被正確釋放,但非托管資源不管,因為非托管資源只能由用戶保證* 1,若用戶手動調用Dispose(),則Dipose(true)被調用,托管與非托管一起釋放了,同時告訴GC不要再執行析構函數* 2,若用戶沒有手動調用Dispose()函數,則析構函數最終會被執行,其中調用了Dipose(false),保證托管資源被釋放*/ public class ConsoleMonitor : IDisposable {private bool disposed = false;public ConsoleMonitor(){Console.WriteLine("The ConsoleMonitor class constructor.\n");}// The destructor calls Object.Finalize.~ConsoleMonitor(){Console.WriteLine("The ConsoleMonitor finalizer.\n");// Call Dispose with disposing = false.Dispose(false);}public void Write(){Console.WriteLine("The Write method.\n");}/**** 不要寫這個方法,會報錯,因為它有BUG,很容易出錯,官方推薦方式是用析構函數來替代此方法* 原理上來說,此方法是繼承了IDispose的類在析構時會被調用的,它最初的設計目的等同于析構函數*///void Finalize()//{//}//給外部手動調用的,方法名可以任意public void Dispose(){Console.WriteLine("TThe Dispose method.\n");Dispose(true);//告訴GC管理器,當GC執行時不要再調用此對象的析構函數(其中有Dispose(false)),因為我們自己已在Dispose(true)中已進行了dipose(false)的操作GC.SuppressFinalize(this); }/***給析構函數(或finalize)調用的,函數簽名必須這樣* 注意finalize已廢棄,見如上說明* disposing:當我們自己調用時傳true,當系統調用時為false* true和false主要為了區別銷毀托管資源與非托管資源*/private void Dispose(bool disposing){Console.WriteLine("The Dispose({0}) method.\n");// Execute if resources have not already been disposed.if (!disposed){// If the call is from Dispose, free managed resources.if (disposing){Console.Error.WriteLine("Disposing of managed resources.");}// Free unmanaged resources.Console.WriteLine("Disposing of unmanaged resources.");}disposed = true;} }public class Example {public static void Main(){Console.WriteLine("ConsoleMonitor instance....");ConsoleMonitor monitor = new ConsoleMonitor();monitor.Write();monitor.Dispose();} } // If the monitor.Dispose method is not called, the example displays the following output: // ConsoleMonitor instance.... // The ConsoleMonitor class constructor. // The Write method. // The ConsoleMonitor finalizer. // The Dispose(False) method. // Disposing of unmanaged resources. // // If the monitor.Dispose method is called, the example displays the following output: // ConsoleMonitor instance.... // The ConsoleMonitor class constructor. // The Write method. // The Dispose method. // The Dispose(True) method. // Disposing of managed resources. // Disposing of unmanaged resources.
                        
                        
                        using System; using System.ComponentModel;/**** 這個模式搞的這么復雜,目的是:不管使用者有沒有手動調用Dipose函數,都能保證托管資源被正確釋放,但非托管資源不管,因為非托管資源只能由用戶保證* 1,若用戶手動調用Dispose(),則Dipose(true)被調用,托管與非托管一起釋放了,同時告訴GC不要再執行析構函數* 2,若用戶沒有手動調用Dispose()函數,則析構函數最終會被執行,其中調用了Dipose(false),保證托管資源被釋放*/ public class ConsoleMonitor : IDisposable {private bool disposed = false;public ConsoleMonitor(){Console.WriteLine("The ConsoleMonitor class constructor.\n");}// The destructor calls Object.Finalize.~ConsoleMonitor(){Console.WriteLine("The ConsoleMonitor finalizer.\n");// Call Dispose with disposing = false.Dispose(false);}public void Write(){Console.WriteLine("The Write method.\n");}/**** 不要寫這個方法,會報錯,因為它有BUG,很容易出錯,官方推薦方式是用析構函數來替代此方法* 原理上來說,此方法是繼承了IDispose的類在析構時會被調用的,它最初的設計目的等同于析構函數*///void Finalize()//{//}//給外部手動調用的,方法名可以任意public void Dispose(){Console.WriteLine("TThe Dispose method.\n");Dispose(true);//告訴GC管理器,當GC執行時不要再調用此對象的析構函數(其中有Dispose(false)),因為我們自己已在Dispose(true)中已進行了dipose(false)的操作GC.SuppressFinalize(this); }/***給析構函數(或finalize)調用的,函數簽名必須這樣* 注意finalize已廢棄,見如上說明* disposing:當我們自己調用時傳true,當系統調用時為false* true和false主要為了區別銷毀托管資源與非托管資源*/private void Dispose(bool disposing){Console.WriteLine("The Dispose({0}) method.\n");// Execute if resources have not already been disposed.if (!disposed){// If the call is from Dispose, free managed resources.if (disposing){Console.Error.WriteLine("Disposing of managed resources.");}// Free unmanaged resources.Console.WriteLine("Disposing of unmanaged resources.");}disposed = true;} }public class Example {public static void Main(){Console.WriteLine("ConsoleMonitor instance....");ConsoleMonitor monitor = new ConsoleMonitor();monitor.Write();monitor.Dispose();} } // If the monitor.Dispose method is not called, the example displays the following output: // ConsoleMonitor instance.... // The ConsoleMonitor class constructor. // The Write method. // The ConsoleMonitor finalizer. // The Dispose(False) method. // Disposing of unmanaged resources. // // If the monitor.Dispose method is called, the example displays the following output: // ConsoleMonitor instance.... // The ConsoleMonitor class constructor. // The Write method. // The Dispose method. // The Dispose(True) method. // Disposing of managed resources. // Disposing of unmanaged resources.
具體理論參考官方解析:
Implementing a Dispose method
總結
以上是生活随笔為你收集整理的有关Dispose,Finalize,GC.SupressFinalize函数-托管与非托管资源释放的模式的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 一个演示A星相关的寻路的网站
 - 下一篇: C++新旧类型转换小记