dotnet / dotnet/docs

Object finalizer rules unclear.

Open
#41,421 0 comments 0 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-fundamentals/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

### Type of issue

Missing information

### Description

The docs seem to indicate that a finalizer can't rely on any of its references being valid & should only access its own members.

> An object's [Finalize](https://learn.microsoft.com/en-us/dotnet/api/system.object.finalize) method shouldn't call a method on any objects other than that of its base class. This is because the other objects being called could be collected at the same time as the calling object, such as in the case of a common language runtime shutdown.

However the intro page for finalize uses a stopwatch methods https://learn.microsoft.com/en-us/dotnet/api/system.object.finalize?view=net-8.0

And the same page also says

> Finalize can take almost any action, including resurrecting an object (that is, making the object accessible again) after it has been cleaned up during garbage collection

These seem to be at odds with the first quoted line.

### Page URL

https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-object-finalize

### Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/runtime-libraries/system-object-finalize.md

### Document Version Independent Id

737ceda6-1a50-eeea-75ad-b3d2f27a732b

### Article author

@gewarren

### Metadata

* ID: 7e033bd4-ad4c-19e8-3ba4-bf563f645dab
* Service: **dotnet-fundamentals**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.