-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Correct how instances may be reachable from a finalizer/dispose #26710
Comments
@IEvangelist Do we want @shadow-cs to add the null-checking info to the "Cascade dispose calls" section? For the first concern, it's not clear to me what the suggested change would be. |
@gewarren - I would be fine with @shadow-cs adding the null-checking info for our review, and I'd ask that @bartonjs help validate the concerns raised here. |
@IEvangelist @gewarren I'll prepare the adjustments so we can discuss them in a PR. I'll be a bit over excessive at first so the intention is clear, and we can refine it later and provide a concise result, in the end, hope it's OK 😉 |
…27073) * Clarify information about reference validity in a finalizer (#26710) * Update docs/standard/garbage-collection/implementing-dispose.md Co-authored-by: David Pine <[email protected]>
The way I understand it there actually are two pieces of information that I find misleading.
Instances (eg. fields) are reachable from a finalizer
GC indeed is non-deterministic as well as the finalizer but fields containing managed instances do not get set to
null
before the finalizer is finished (at which point the object graph starting with the finalized instance may get reclaimed).@Maoni0 stated in a blog post:
So not only the instances being finalized but also all references there should be live, no?
Null-checking in a
Dispose
callIn the sample of Cascade dispose calls a null checking is used but it may not be clear to the reader why.
In this sample
null
may never end-up in the_bar
because in caseBar
ctor throws an exception, nothing is returned and the caller has no way to callDispose
thus making the check a no-op.However, there are two ways a null-checking is actually the desired pattern in a
Dispose
method:Dispose
(in afinally
block perhaps) to clean after itself when things go wrong (ie. exception is thrown from the ctor) - not the best pattern to use, but this can cause problem if the programmer doesn't realize that soon enough.There were even more samples with null-checking which was added (#24259) and later removed (#26192).
I'll be happy to update the docs if desirable, please provide feedback on how much information should be put in the docs.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: