Finalization
lượt xem 1
download
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;...
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Finalization
- Finalization
- Objectives • Describe options for handling unmanaged resources – destructor / Finalize – Dispose / IDisposable / using 2
- Resources • Program may use many different unmanaged resources – disk file – mutex lock – socket connection – database connection 3
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- Order of finalization • Finalization order of separate objects not guaranteed Department File object object may be finalized in any order 18
- 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
- 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
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Tổng quan Plugins Final Affter Effect
11 p | 321 | 159
-
Tổng quan Plugins Final After Effect
11 p | 432 | 144
-
Ghost Win7 U-SP1(32bit) Full Soft V2 Final– Bản ghost hoàn hảo tung chảo năm 2011
8 p | 440 | 69
-
Cài đặt Plug-Ins Final After Efftect
6 p | 244 | 56
-
CCNA1 Practice Final H
18 p | 176 | 34
-
SpeedFan 4.39 Final - Điều chỉnh tốc độ quạt và overlock hệ thống
10 p | 359 | 30
-
Hướng dẫn crack WinRAR 3.93 Final | Nén và giải nén hàng đầu thế giới
6 p | 299 | 27
-
WindowBlinds 7 Final: “bậc thầy” làm đẹp Windows
6 p | 128 | 21
-
Trải nghiệm Firefox 4 Final vừa lên sóng
7 p | 88 | 10
-
Trải nghiệm làn gió mới Yahoo! Messenger 11 Final
16 p | 51 | 9
-
Chapter 18: Final Submission and Essay
11 p | 80 | 6
-
FWL Skills Final
6 p | 47 | 5
-
Trải nghiệm Firefox 6.0 Final - Bản nâng cấp hoàn hảo
3 p | 65 | 5
-
10 lý do giúp Firefox 4 được tải về nhiều hơn IE9
4 p | 54 | 4
-
Khắc phục lỗi cài đặt Internet Explorer 9 Final
5 p | 248 | 4
-
Using a finally Block
2 p | 66 | 2
-
Final examination semester 2 academic year 2019-2020 course name: Design of steel structures II - ĐH Sư phạm Kỹ thuật
2 p | 21 | 2
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn