This rule is Obsolete | |
Synopsis: | Provide a method that will cause the finalizer not to be called |
Language: | C# |
Severity Level: | 3 |
Category: | Object lifecycle |
Description: |
If a finalizer is needed to verify that a user has called certain cleanup methods, call
public class IpcPeer { bool connected = false; public void Connect() { // Do some work and then change the state of this object. connected = true; } public void Close() { // Close the connection, change the state, and instruct garbage collector // not to call the finalizer. connected = false; GC.SuppressFinalize(this); } ~IpcPeer() { // If the finalizer is called, then Close() was not called. if (connected) { // Warning! User has not called Close(). Notice that you can't // call Close() from here because the objects involved may // have already been garbage collected (see Rule 5@113). } } } |