Skip to content

Add warning about accessing managed resources in finalizers #47706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 1, 2025

The finalizers documentation was missing important guidance about the safety of accessing managed object members from finalizers. This could lead developers to write unsafe code that attempts to access managed objects that may have already been disposed.

This change adds a warning in the "Using finalizers to release resources" section that clearly advises developers against accessing managed object members from finalizers. The warning explains that managed objects might already be disposed during finalization, making them unavailable or in an invalid state, and emphasizes that finalizers should only access unmanaged resources directly.

The warning follows Microsoft documentation standards using the [!WARNING] callout format and is positioned appropriately in the section that discusses what finalizers should be used for.

Fixes #31199.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/programming-guide/classes-and-structs/finalizers.md docs/csharp/programming-guide/classes-and-structs/finalizers

@Copilot Copilot AI changed the title [WIP] Cleanup of managed resources in the finalizer? Add warning about accessing managed resources in finalizers Aug 1, 2025
@Copilot Copilot AI requested a review from BillWagner August 1, 2025 19:36
Copilot finished work on behalf of BillWagner August 1, 2025 19:36
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small technical edit, and then this is ready

@dotnet-policy-service dotnet-policy-service bot added the okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. label Aug 4, 2025
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ready for final review.

@BillWagner BillWagner marked this pull request as ready for review August 4, 2025 18:16
@BillWagner BillWagner requested a review from a team as a code owner August 4, 2025 18:16
@BillWagner BillWagner requested a review from tdykstra August 4, 2025 18:16
@Copilot Copilot AI requested a review from BillWagner August 4, 2025 18:16
Copilot finished work on behalf of BillWagner August 4, 2025 18:16
@BillWagner BillWagner requested a review from a team August 4, 2025 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc fundamentals/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cleanup of managed resources in the finalizer?
2 participants