For managed objects, object destruction and memory reclamation are decoupled. This supports reliable code in two ways. First, accessing managed objects that are already destroyed cannot corrupt the state of some other random object; instead, it often ends up in a well-defined reaction—the ObjectDisposedException. The second benefit is that the runtime can call the finalizer to perform last-chance cleanup before memory of an undestructed object is reclaimed. This is helpful to ensure reliable cleanup of non-memory resources, but it can also be the source of pitfalls like the finalization timing race condition or the graph promotion problem. Furthermore, normal finalizers are often not reliable enough for long-running servers like SQL Server 2005. To further increase reliability, the CLR version 2.0 introduces CERs, which ensure that regions of constrained code cannot be interrupted by an asynchronous exception. One way to avoid these pitfalls and to benefit from CERs is the use of the SafeHandle base class.
Was this article helpful?