Skip to content

Fix leak in UpDownBaseTests #13597

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3086,6 +3086,8 @@ private class CustomValidateUpDownBase : UpDownBase

public class SubUpDownBase : UpDownBase
{
private int _handleCreatedCallCount;

public new const int ScrollStateAutoScrolling = ScrollableControl.ScrollStateAutoScrolling;

public new const int ScrollStateHScrollVisible = ScrollableControl.ScrollStateHScrollVisible;
Expand Down Expand Up @@ -3191,7 +3193,11 @@ public override void DownButton()

public new void OnFontChanged(EventArgs e) => base.OnFontChanged(e);

public new void OnHandleCreated(EventArgs e) => base.OnHandleCreated(e);
public new void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
_handleCreatedCallCount++;
}

public new void OnHandleDestroyed(EventArgs e) => base.OnHandleDestroyed(e);

Expand Down Expand Up @@ -3227,6 +3233,31 @@ public override void DownButton()

public new void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) => base.SetBoundsCore(x, y, width, height, specified);

protected override void Dispose(bool disposing)
{
// See https://github.com/dotnet/winforms/issues/13596.
// Some tests call OnHandleCreated (even multiple times!), but doesn't really create handle.
// In this case, we are subscribing to UserPreferenceChanged (multiple times!).
// We need to call OnHandleDestroyed to avoid leaks.
// We need to call it the "right" number of times though.
// We start with _handleCreatedCallCount.
var destroyCount = disposing ? _handleCreatedCallCount : 0;

if (IsHandleCreated)
{
// If a "real" handle was created, base.Dispose will destroy.
// So, we subtract 1.
destroyCount--;
}

for (int i = 0; i < destroyCount; i++)
{
OnHandleDestroyed(EventArgs.Empty);
}

base.Dispose(disposing);
}

public override void UpButton()
{
throw new NotImplementedException();
Expand Down