Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding.RemovePropertyEvent does not remove the 'EventHandler' subscription #2446

Open
Dmitri-Kom opened this issue Mar 17, 2023 · 0 comments · May be fixed by #2644
Open

Binding.RemovePropertyEvent does not remove the 'EventHandler' subscription #2446

Dmitri-Kom opened this issue Mar 17, 2023 · 0 comments · May be fixed by #2644
Labels
Milestone

Comments

@Dmitri-Kom
Copy link
Contributor

Dmitri-Kom commented Mar 17, 2023

Hi there,

Recently, I've upgraded to the 2.7.3 version, where I also contributed via this attached PR.

Unfortunately, the leak was not resolved, even with my above contribution. I'll try to explain the reason for that.

In order to reproduce, you can just chose a random class and add in the constructor / some initialization method the following lines:

Binding.AddPropertyEvent(viewModel, vm => vm.Checked, OnCheckedChanged);
Binding.RemovePropertyEvent(viewModel, OnCheckedChanged);

Expected Behavior

After writing the above lines, we expect the OnCheckedChanged event handler to never be invoked, as we don't have a subscription to it, as we've invoked Binding.RemovePropertyEvent for it.

Actual Behavior

The OnCheckedChanged event handler is still invoked, even after calling Binding.RemovePropertyEvent for it (the breakpoint is hit).

Specifications

  • Version: 2.7.3
  • Platform(s): WPF
  • Operating System(s): Windows 10, Windows 11

Reason for the issue:

In 2.7.3, your source code for the Binding.RemovePropertyEvent looks as following:

public static void RemovePropertyEvent(object obj, EventHandler<EventArgs> eh)
{
	var helper = eh.Target as PropertyNotifyHelper;
	if (helper != null)
	{
		helper.Changed -= eh;
		helper.Unregister(obj);
	}
}

Previously, I've contributed by adding the helper.Changed -= eh; line, but surprisingly, as I've mentioned, the leak is still there, and the event handler keeps getting invoked, even after calling this RemovePropertyEvent method.

After investigating a bit, I've discovered that the helper is null, because eh.Target is not of type PropertyNotifyHelper , but of the type of the class, where I've defined the event handler (i.e. CustomCheckBox.cs). Thus, this code:

helper.Changed -= eh;
helper.Unregister(obj);

never happens, and the subscription is kept alive.

I couldn't find any issue about it, so was wondering if you are aware of that.

A possible direction is to understand why eh.Target stopped working for WPF (assuming it used to work).

@cwensley cwensley added the bug label Jun 26, 2023
@cwensley cwensley added this to the 2.8.x milestone Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants