Memory leak issue in WPF custom textbox control

Saravanan Ganesn 11 Reputation points

Hi Team,

I have created custom textbox control which was derived from TextBox control. In CustomTextBox, the memory leak exists even after disposing all the instances of CustomTextBox because base class (TextBox) instances still persist. In the attached sample, I have disposed the CustomTextBox instance on window closed event. I have used the Ants Memory Profiler tool to find the memory leak and I have attached an image which shows the TextBox memory leak report. Please check and do the needful.


Sample: s!AsISs7GoFbYYgsFLKG3cq1WSuM3zeg



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,698 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. EckiS 826 Reputation points

    Your profiler should show you what objects are kept alive, and who is keeping references.
    But your screenshot doesn't display any of this.

    And why would you need to unhook from events of the same instance?
    And why would you set a string to String.Empty in Dispose? What is this supposed to achieve?

    I don't see a memory leak on my system (removed the useless Dispose), showing and closing the dialog 100 time.
    (at least the Visual Studio Memory Profiler doesn't find an increase in objects after I forced GC)

     private void Button_Click(object sender, RoutedEventArgs e)
                for (int i = 0; i < 100; i++)
                    var window1 = new Window1();
                    window1.Owner = this;
                    window1.ShowInTaskbar = false;
                    window1.ContentRendered += (_, __) => window1.Close();
    1 person found this answer helpful.

  2. EckiS 826 Reputation points

    I don't understand why your CurrencyTextBox even implements IDispose. Nothing you do in your Dispose method is needed.

  3. Michael 41 Reputation points

    There is something that cam look like memory link in textbox but really isn't. Textbox has an undo feature. The longer the textbox is around the larger the undo buffer can grow. You can turn off the undo feature and it will stop growing. This was explored extensively in the forums several years ago.

    0 comments No comments