Download presentation
Presentation is loading. Please wait.
1
Finalization 17: Finalization
Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
2
Objectives Describe options for handling unmanaged resources
17: Finalization Objectives Describe options for handling unmanaged resources destructor / Finalize Dispose / IDisposable / using Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
3
Resources Program may use many different unmanaged resources disk file
17: Finalization Resources Program may use many different unmanaged resources disk file mutex lock socket connection database connection Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
4
Unmanaged resource Typical to wrap unmanaged resource in class
17: Finalization Unmanaged resource Typical to wrap unmanaged resource in class wrapper deals with complexities client code uses convenient methods class File { public File(string name) { ... } public char Read () { ... } public void Write(char c) { ... } public void Close() { ... } } open access close handle details of resource interaction Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
5
Use of unmanaged resource
17: Finalization 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(); ... f.Close(); } must call close method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
6
Destructor Class can supply destructor to release unmanaged resource
17: Finalization 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 { ~File() if (IsOpen()) Close(); } ... destructor Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
7
Destructor invocation
17: Finalization 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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
8
Destructors and inheritance
17: Finalization 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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
9
Finalize C# destructor compiled into override of object.Finalize()
17: Finalization 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() try Flush(); } finally base.Finalize(); ... class BufferedFile : File { ~BufferedFile() Flush(); } ... Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
10
Destructor limitations
17: Finalization 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 { ~File() { ... } public void Close() { ... } ... } destructor release method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
11
Client responsibility
17: Finalization 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); ... f.Close(); } client code should release resource Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
12
IDisposable Client release of unmanaged resource formalized
17: Finalization IDisposable Client release of unmanaged resource formalized implement IDisposable.Dispose method simplifies client's task by defining standard method signature class File : IDisposable { public void Dispose() if (IsOpen()) Close(); } ... Dispose void Process(string name) { File f = new File(name); ... f.Dispose(); } client invokes Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
13
Exception safety Clients should call Dispose even if exception thrown
17: Finalization Exception safety Clients should call Dispose even if exception thrown ensures cleanup in all cases void Process(string name) { File f = new File(name); ... f.Dispose(); } might be missed if exception thrown Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
14
Using A using statement automatically calls Dispose
17: Finalization 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 Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
15
Destructor and Dispose
17: Finalization 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() ... GC.SuppressFinalize(this); } suppress destructor Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
16
Owning managed resource
17: Finalization Owning managed resource Object may own a managed resource fields may be references to other objects class Department { File employees = new File("Employees.txt"); ... } owned object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
17
Eligibility for finalization
Object and owned resources typically eligible for finalization at same time void Pay() { Department it = new Department(); ... } owns a file Department and File objects both eligible for finalization Department object File object it Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
18
17: Finalization Order of finalization Finalization order of separate objects not guaranteed Department object File object may be finalized in any order Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
19
Destructor and managed resource
17: Finalization 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() employees.Close(); } ... should not access in destructor Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
20
Owned object requiring cleanup
17: Finalization 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() employees.Close(); } ... close file Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
21
Summary Management of non-memory resources problematic
17: Finalization Summary Management of non-memory resources problematic language and library offer some help but no foolproof way to always release Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.