Memory Leak on ToolTip

James Pike 0 Reputation points
2023-09-19T14:42:34.43+00:00

In our product we are using a ribbon control with tool tips and it appears the tooltips are leading to memory leaks.

I've been able to repeat with a simple sample project (attached as a .zip but renamed as .txt to allow attaching).

It's simply a ribbon with a few buttons:

<ribbon:RibbonWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                       xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                      x:Class="MemoryLeak.MainWindow" Title="MainWindow" Height="350" Width="525">          <ribbon:Ribbon>         <ribbon:RibbonTab Header="Home">             <ribbon:RibbonGroup Header="Font">                 <ribbon:RibbonButton Label="Change DataContext"                                       Click="ButtonBase_OnClick"                                       ToolTip="This tooltip does not leak" />                 <ribbon:RibbonButton Label="{Binding Path=Number}" />                 <ribbon:RibbonButton x:Name="RunRiskAnalysisMenuTool"                                       Label="RunRiskAnalysis}">                     <!-- ToolTip -->                     <ribbon:RibbonButton.ToolTip>                         <ToolTip Content="This tooltip leaks" />                     </ribbon:RibbonButton.ToolTip>                 </ribbon:RibbonButton>             </ribbon:RibbonGroup>         </ribbon:RibbonTab>     </ribbon:Ribbon> </ribbon:RibbonWindow>

One of which changes the ViewModel and then I've added code to force GC to ensure GC isn't the issue.

public partial class MainWindow {              public MainWindow() {       InitializeComponent();       DataContext = new ViewModel();     }          private void ButtonBase_OnClick(object sender, RoutedEventArgs e)         {             DataContext = new ViewModel();              GC.Collect();             GC.WaitForPendingFinalizers();             GC.Collect();         }    }

Using RedGate or dotMemory I can see two instance of ViewModel are around when there should only ever be one, and that one is being held onto by the tool tip.

2023-09-19_9-31-30

I've found this link reporting the same issue, but with no resolution: https://learn.microsoft.com/en-us/answers/tags/147/windows-wpf

Any help would be appreciated. For now we've taken the approach of adding a lot of disposal code to try to clear out as much of the leaking memory as we can, but a solution to the bug would help us a lot.

Note: In the example I am using just a ToolTip but we use a RibbonToolTip because we want the header/description type display. I simplified the example because I assume RibbonToolTip is built off ToolTip and probably is the same leak.

ToolTip Ribbon Memory Leak.txt

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,785 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.