0

I was creating a C# WPF custom control library, My library uses DisplaySettingsChanged event from SystemEvents Class, In the Docs Microsoft mentioned about detaching the Event Handler.

Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.

So will it be possible to use lambda expression without causing memory leak in the program ( without using -= ).

Which means :

Can I use this:

SystemEvents.DisplaySettingsChanged += (_, _) =>
{
   // My code Here
};

instead of using this:

SystemEvents.DisplaySettingsChanged += OnDisplaySettingsChanged;
SystemEvents.DisplaySettingsChanged -= OnDisplaySettingsChanged; // Calling this in Dispose Method

Full Code

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Interop;

namespace FluentCompositor.Core
{
    public sealed class CompositionMetrics : IDisposable
    {
        private readonly HwndSource hwndSource;

        public CompositionMetrics(HwndSource source)
        {
            hwndSource = source;
            SystemEvents.DisplaySettingsChanged += OnDisplaySettingsChanged; //added event handler over here
        }

        private void OnDisplaySettingsChanged(object? sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            SystemEvents.DisplaySettingsChanged -= OnDisplaySettingsChanged; // disposing event handler here
        }

        #region Properties

        public int Width
        {
            get;
            private set;
        }

        public int Height
        {
            get;
            private set;
        }

        public double ScaleFactor
        {
            get;
            private set;
        }

        #endregion
    }
}
trickymind
  • 557
  • 5
  • 21
  • Docs explicitly say that you must unsubscribe or memory leak will occur, and you ask if you can ignore unsubscribe without causing memory leak? – Evk Nov 10 '21 at 12:17

1 Answers1

1

You can't. Lambda is anonymous, so you will not able to detach event and thus will cause a memory leak.

I think, you can store you lambda in the class-level field, and then use this value to detach event, but it is almost the same as the approach with the ordinal handler without lambda.

Serg
  • 3,454
  • 2
  • 13
  • 17
  • Thank you, is detaching the event handler in dispose() is an appropriate method ? – trickymind Nov 10 '21 at 15:35
  • 1
    Yes, detaching in the `Dispose` is absolutely appropriate. Just take into account that it is absolutely up to developer's responsibility to call `Dispose` for the object. And if developer will not call it, you will have a leak. – Serg Nov 10 '21 at 15:53
  • Sure I am using this class for internal use and it won't be exposed outside the library. i will use internal access specifier and will call Dispose on the class where I use the object of CompositionMetrics. Thankyou – trickymind Nov 10 '21 at 16:02