Proposal: Add support for weak delegates in core runtime #130662
-
|
The Problem I am seeing lots of memory leaks rooting to events in application development. The fix for these issues is not small and sometimes the code changes are big and complex. What if we could have a WeakDelegate? A weakly referenced delegate would solve this pain point entirely. When a short-lived object subscribes to an event on a long-lived object, it shouldn't stop the garbage collector from collecting the referencee. Proposed Solution Introduce a new type in the .NET core runtime called WeakDelegate. Type Specification
Proposed C# Syntax Options We can approach this in two ways: Option 1: The
Option 2: The
Prior Implementation There was already a type named WeakDelegate implemented in the .NET Micro Framework. Since that framework is discontinued and the implementation was a specialized workaround for small embedded systems, this proposal seeks a first-class integration into the modern .NET core runtime. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Related to #99308 and https://learn.microsoft.com/dotnet/api/system.windows.weakeventmanager .
It would just be some wrapper around
This will definitely not happen. Even
If there is no direct support by GC (for |
Beta Was this translation helpful? Give feedback.
-
|
Thanks, I see you also have a proposal for these .NET shortcomings.
But why not do it? This feels like a fundamental feature that deserves to be built right into the language. |
Beta Was this translation helpful? Give feedback.
Related to #99308 and https://learn.microsoft.com/dotnet/api/system.windows.weakeventmanager .
It would just be some wrapper around
WeakReference<TDelegate>. The composition wouldn't be a new type.This will definitely not happen. Even
WeakReferenceis not a first class concept in the type system.If there is no …