Skip to content

Commit

Permalink
Revert dotnet#4808 fix. Due to dotnet#12591 and investigations.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirsan31 committed Jan 8, 2025
1 parent aaac41d commit 87a65cb
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4680,16 +4680,9 @@ internal void OnItemAddedInternal(ToolStripItem item)
}
}

internal void OnItemRemovedInternal(ToolStripItem item, ToolStripItemCollection itemCollection)
internal void OnItemRemovedInternal(ToolStripItem item)
{
KeyboardToolTipStateMachine.Instance.Unhook(item, ToolTip);
if (itemCollection == _toolStripItemCollection)
{
// To prevent memory leaks when item removed from main collection,
// we need to remove it from _displayedItems and _overflowItems too.
_displayedItems?.Remove(item);
_overflowItems?.Remove(item);
}
}

internal override bool AllowsChildrenToShowToolTips()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ private void OnAfterRemove(ToolStripItem item)

if (_owner is not null)
{
_owner.OnItemRemovedInternal(item, this);
_owner.OnItemRemovedInternal(item);

if (!_owner.IsDisposingItems)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7337,37 +7337,6 @@ void TimerStartAndItemDispose()
}
}

[WinFormsFact]
public void ToolStrip_displayedItems_Clear()
{
using ToolStripMenuItem toolStripMenuItem = new(nameof(toolStripMenuItem));
using ToolStripMenuItem listToolStripMenuItem = new(nameof(listToolStripMenuItem));
toolStripMenuItem.DropDownItems.Add(listToolStripMenuItem);
toolStripMenuItem.DropDownOpened += (sender, e) =>
{
for (int i = 0; i < 4; i++)
listToolStripMenuItem.DropDownItems.Add("MenuItem" + i);

listToolStripMenuItem.DropDown.PerformLayout(); // needed to populate DisplayedItems collection
};

toolStripMenuItem.DropDownClosed += (sender, e) =>
{
while (listToolStripMenuItem.DropDownItems.Count > 0)
listToolStripMenuItem.DropDownItems[listToolStripMenuItem.DropDownItems.Count - 1].Dispose();

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
};

toolStripMenuItem.ShowDropDown();
Assert.Equal(4, listToolStripMenuItem.DropDown.DisplayedItems.Count);
toolStripMenuItem.HideDropDown();
Assert.Empty(listToolStripMenuItem.DropDown.DisplayedItems);
}

[WinFormsTheory]
[InlineData(10, 10)]
[InlineData(0, 0)]
Expand Down

0 comments on commit 87a65cb

Please sign in to comment.