I suggest you ...

Provide simple syntax to create a weak-referenced eventhandler

The by far most common memory leak people introduce in .NET apps are caused by event handlers added to longer-lived objects. For instance an event on a static instance, or on an instance who's lifetime is the entire app lifetime (like app view model or app settings).
Listening for events on this from a shorter-lived object, makes this short-lived object hang around in memory until the app stops running. A common scenario is the INotifyCollectionChanged event listened to by a XAML control, with data coming from a collection provided by some app state. This will cause a very memory-consuming object like a UIElement to stay around in memory until the app closes. The recommended pattern to avoid this is by using WeakReference preferable wrapped in an event manager / event proxy class. However this can be quite a lot of code to write, and the implementation is not straight forward, and will still cause the small proxy hang around at least until the event fires again (if it ever does).

It would be much simpler if a syntax alternative to "+=" would denote "weak". For example:
staticInstance.Data.CollectionChanged += MyChangedHandler; //not weak event reference
staticInstance.Data.CollectionChanged +~ MyChangedHandler; //weak event reference

I'm not saying '+~' should actually be the syntax - just that we have something as simple as this to solve a very common scenario.

Also see: https://github.com/dotnet/roslyn/issues/101

132 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Morten NielsenMorten Nielsen shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    4 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Frédéric DecréquyFrédéric Decréquy commented  ·   ·  Flag as inappropriate

        In the way, it can be interesting to search on the possibility to define the thread which used to execute the event, if it isn't the same.

      • Morten NielsenMorten Nielsen commented  ·   ·  Flag as inappropriate

        @lex a new keyword like this is far more reaching than adding an extra event subscription syntax (my suggestion wasn't operator overload). Ie what does this mean:
        object obj = weak new Object();
        Action myAction = weak () => { } //anonymous delegates instantly GC'ed ?

        Btw I suggest discussion points be made on the GitHub issue

      • Lex LavnikovLex Lavnikov commented  ·   ·  Flag as inappropriate

        staticInstance.Data.CollectionChanged += MyChangedHandler; //not weak event reference
        staticInstance.Data.CollectionChanged += weak MyChangedHandler; //weak event reference

        No need to introduce new operator (+~), due to overloading hassle, just new keyword "weak". It could also be used in event declaration:

        public weak event EventHandler OnClick; // all delegates will be weak subscribed

      Feedback and Knowledge Base