intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Finalization

Chia sẻ: Lavie Lavie | Ngày: | Loại File: PDF | Số trang:0

34
lượt xem
1
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Objectives of the Finalization is describe options for handling unmanaged resources (destructor/Finalize, Dispose/IDisposable/using). It is includes resources; Unmanaged resource; Use of unmanaged resource; Destructors and inheritance;...

Chủ đề:
Lưu

Nội dung Text: Finalization

  1. Finalization
  2. Objectives • Describe options for handling unmanaged resources – destructor / Finalize – Dispose / IDisposable / using 2
  3. Resources • Program may use many different unmanaged resources – disk file – mutex lock – socket connection – database connection 3
  4. Unmanaged resource • Typical to wrap unmanaged resource in class – wrapper deals with complexities – client code uses convenient methods class File { open public File(string name) { ... } public char Read () { ... } access public void Write(char c) { ... } close public void Close() { ... } } handle details of resource interaction 4
  5. Use of unmanaged resource • Must arrange for release of unmanaged resource – CLR does not handle automatically – responsibility placed on client – error prone since client could forget void Process(string name) { File f = new File(name); char c = f.Read(); ... must call f.Close(); close method } 5
  6. Destructor • Class can supply destructor to release unmanaged resource – same name as class preceded by ~ – no argument – no return type – no access modifier – cannot be overloaded class File { destructor ~File() { if (IsOpen()) Close(); } ... } 6
  7. Destructor invocation • Destructor called automatically during garbage collection – not called immediately when object no longer in use – cannot be called explicitly void Process(string name) { File f = new File(name); char c = f.Read(); ... destructor not called } until garbage collection 7
  8. Destructors and inheritance • Destructor automatically calls base class destructor – call occurs at end of destructor body – ensures each class has chance to release resources class File { ~File() { ... } ... } class BufferedFile : File { ~BufferedFile() { ... calls ~File } automatically ... } 8
  9. Finalize • C# destructor compiled into override of object.Finalize() – ensures compatibility with other .NET languages – includes chain to base Finalize – call to base put in finally block to make code exception safe – C# code not allowed to override Finalize directly class BufferedFile : File { protected override void Finalize() { class BufferedFile : File try { { ~BufferedFile() Flush(); { } Flush(); finally } { ... base.Finalize(); } } } ... } 9
  10. Destructor limitations • Destructors typically not sufficient to release resources – not called in specific order – may not be called in timely manner – may not ever be called in some circumstances • Common to supply method to do resource release – many programmers avoid destructor due to its many limitations class File { destructor ~File() { ... } release method public void Close() { ... } ... } 10
  11. Client responsibility • Clients typically release unmanaged resources explicitly – allows control over timing of resource release – not common to rely on destructor even if one is provided void Process(string name) { File f = new File(name); ... client code should f.Close(); release resource } 11
  12. IDisposable • Client release of unmanaged resource formalized – implement IDisposable.Dispose method – simplifies client's task by defining standard method signature class File : IDisposable { Dispose public void Dispose() { if (IsOpen()) Close(); } ... } void Process(string name) { File f = new File(name); ... client invokes f.Dispose(); } 12
  13. Exception safety • Clients should call Dispose even if exception thrown – ensures cleanup in all cases void Process(string name) { File f = new File(name); ... might be missed if exception thrown f.Dispose(); } 13
  14. Using • A using statement automatically calls Dispose – syntax: keyword using, object to use, code block – ensures timely clean up of unmanaged resource – called even if exception thrown using (File f = new File()) { ... Dispose called } File f = new File(); using (f) { ... Dispose called } 14
  15. Destructor and Dispose • No need to call destructor if Dispose has been called – resource already released – calling destructor would just waste time • Can tell garbage collector to skip finalization of object – use GC.SuppressFinalize() – typically call from Dispose class File : IDisposable { public void Dispose() { ... suppress GC.SuppressFinalize(this); destructor } ... } 15
  16. Owning managed resource • Object may own a managed resource – fields may be references to other objects class Department { owned File employees = new File("Employees.txt"); object ... } 16
  17. Eligibility for finalization • Object and owned resources typically eligible for finalization at same time void Pay() { owns a file Department it = new Department(); Department and ... File objects both } eligible for finalization Department File it object object 17
  18. Order of finalization • Finalization order of separate objects not guaranteed Department File object object may be finalized in any order 18
  19. Destructor and managed resource • Should not access managed object in destructor – resource may already have been finalized class Department { File employees = new File("Employees.txt"); ~Department() should not { access in employees.Close(); destructor } ... } 19
  20. Owned object requiring cleanup • Should arrange for cleanup of owned objects that need it – provide method user must call such as Close or Dispose – ok to access owned resource since called while object still live class Department { File employees = new File("Employees.txt"); public void Close() { close file employees.Close(); } ... } 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2