Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

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.
(one of many examples dotnet/maui#36274)

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

  • Type name: System.WeakDelegate
  • The main difference from a normal Delegate is that the reference to the target object should be a weak reference.
  • It must not prevent the Garbage Collector from collecting the object instance it points to.
  • Checking if the delegate is assigned should not change. Developers should be able to check for null exactly as they do today, and that should work perfectly.

Proposed C# Syntax Options

We can approach this in two ways:

Option 1: The delegate? operator

public delegate? void MyEvent();

  • No new keywords are introduced to the language.
  • It is easy for developers to understand based on existing nullability patterns.

Option 2: The weakdelegate keyword

public weakdelegate void MyEvent();

  • Gives a clear definition of the exact intent of the developer.
  • Explicitly states that the reference chain is weak, though it requires a new keyword.

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.

You must be logged in to vote

Related to #99308 and https://learn.microsoft.com/dotnet/api/system.windows.weakeventmanager .

Introduce a new type in the .NET core runtime called WeakDelegate.

It would just be some wrapper around WeakReference<TDelegate>. The composition wouldn't be a new type.

Proposed C# Syntax Options

This will definitely not happen. Even WeakReference is not a first class concept in the type system.

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.

If there is no …

Replies: 2 comments · 1 reply

Comment options

Related to #99308 and https://learn.microsoft.com/dotnet/api/system.windows.weakeventmanager .

Introduce a new type in the .NET core runtime called WeakDelegate.

It would just be some wrapper around WeakReference<TDelegate>. The composition wouldn't be a new type.

Proposed C# Syntax Options

This will definitely not happen. Even WeakReference is not a first class concept in the type system.

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.

If there is no direct support by GC (for List<WeakReference<TDelegate>>, mentioned in #99308), it will be simply worse than composited WeakReference<TDelegate>.

You must be logged in to vote
0 replies
Answer selected by salarcode
Comment options

Thanks, I see you also have a proposal for these .NET shortcomings.
This is such a common issue.
it feels like the core team doesn't see this as a priority, or maybe the under-the-hood changes are just too massive for them to consider.

This will definitely not happen. Even WeakReference is not a first class concept in the type system.

But why not do it? This feels like a fundamental feature that deserves to be built right into the language.

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

But why not do it? This feels like a fundamental feature that deserves to be built right into the language.

A new concept in the type system has extremely high bar. It needs to be not achievable by other ways, and being fundamental enough. Only ref struct meets the bar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.