Skip to content

Why WeakReference doesn't work? #112779

Answered by EgorBo
Li7ye asked this question in Q&A
Feb 21, 2025 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

This is not guaranteed to work the way you expect it, especially when it's called in the same method as it depends on the optimizations level (Tier0/Tier1) of the current method (namely, precise liveness analysis) and whether GC in general is precise or not on the given runtime. You may rewrite your repro to call TryGetTarget in different methods or put [MethodImpl(MethodImplOptions.AggressiveOptimization)] on the current method, but it's better to not depend on this anyway.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Li7ye
Comment options

Answer selected by Li7ye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants