Skip to content

Commit b648936

Browse files
CopilotBillWagner
andauthored
Add warning about accessing managed resources in finalizers (#47706)
* Initial plan * Add warning about accessing managed resources in finalizers Co-authored-by: BillWagner <[email protected]> * Fix technical inaccuracy: garbage collector doesn't call dispose Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]>
1 parent 29a868d commit b648936

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

docs/csharp/programming-guide/classes-and-structs/finalizers.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ The programmer has no control over when the finalizer is called; the garbage col
5959

6060
In general, C# does not require as much memory management on the part of the developer as languages that don't target a runtime with garbage collection. This is because the .NET garbage collector implicitly manages the allocation and release of memory for your objects. However, when your application encapsulates unmanaged resources, such as windows, files, and network connections, you should use finalizers to free those resources. When the object is eligible for finalization, the garbage collector runs the `Finalize` method of the object.
6161

62+
> [!WARNING]
63+
> Don't access managed object members from a finalizer. During finalization, managed objects might already be disposed, making them unavailable or in an invalid state. Only access unmanaged resources directly from finalizers.
64+
6265
## Explicit release of resources
6366

6467
If your application is using an expensive external resource, we also recommend that you provide a way to explicitly release the resource before the garbage collector frees the object. To release the resource, implement a `Dispose` method from the <xref:System.IDisposable> interface that performs the necessary cleanup for the object. This can considerably improve the performance of the application. Even with this explicit control over resources, the finalizer becomes a safeguard to clean up resources if the call to the `Dispose` method fails.

0 commit comments

Comments
 (0)