Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 537977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:59:01+00:00 2026-05-13T09:59:01+00:00

I have an application I am making that creates a large number of windows

  • 0

I have an application I am making that creates a large number of windows controls (buttons and labels etc). They are all being made dynamically through functions. The problem I’m having is, when I remove the controls and dispose them, they are not removed from memory.

void loadALoadOfStuff()
{
    while(tabControlToClear.Controls.Count > 0)
        tabControlToClear.Controls[0].Dispose();
    //I even put in:
    GC.Collect();
    GC.WaitForPendingFinalizers();
    foreach(String pagename in globalList)
        tabControlToClear.Controls.Add(MakeATab(pagename));
}

TabPage MakeATab(string tabText)
{
    TabPage newT = new MakeATab();
    newT.Text = tabText;
    //Fill page with controls with methods like this
    return newT;
}

Now for some reason, this just ain’t giving me my memory back, so when the process runs 5 times, I end up with an out of memory violation. I am new to object and control disposal but looking through the vast net still hasn’t given me any indications, so if any of you have an idea, I’d be grateful to hear it.

UPDATE: I’ve been watching the user objects creation and destruction (taskmanager) and noticed I create a tab page, add a click handler, add a panel, add 2 buttons both with click handlers, tooltips and backimages (I think this is where the problem is). The app says it creates 8 new items, but when I run through my dispose, I only remove 4 from memory. I’ve been trying to remove the event handlers, but it seems to make no difference.

RESOLVED!!! As I was adding new items to the panel, I was passing them a tooltip (stupid, but I’m learning). For anyone else who has the same problem, (thanks to comments and directions from people below. I discovered in order to make a control truly dispose (as I realise I so incorrectly put it) is:

1: IF YOU HAVE A TOOL TIP, MAKE SURE IT’S ACCESSABLE! DO NOT DO WHAT I DID! E.g:

This is WRONG!

TabPage MakeATab(string tabText)
{
    TabPage newT = new MakeATab();
    ToolTip myTip = new ToolTip();
    newT.Text = tabText;
    //Fill page with controls with methods like this
    myTip.SetToolTip(newT, "Something to say");
    return newT;
}

If you do this, you will lose the pointer to the tooltip, and as the tooltip is not a child of the object it’s connected to (rather, the tooltip makes a strong reference to the control), then even if you destroy the control, the tooltip that you can’t access keeps the object alive.

2: Before anything, call toolTip.RemoveAll(). This removes all it’s ties to controls. Note, if you were using this tip for other controls, they just lost their tool tip.

3: Remove any internal controls from the base control.ControlCollection (if they use non managed memory, I guess. I’m doing it cause it’s making my app work so…)

4: remove any custom event handlers.

5: finally, dispose the object. I made a quick recursing function that does it quite well.

    private void RecursiveDispose(Control toDispose)
    {
        while (toDispose.Controls.Count > 0)
            RecursiveDispose(toDispose.Controls[0]);

        if (toDispose.BackgroundImage != null)
            BackgroundImage = null;

        if (toDispose.GetType() == typeof(Button))
            toDispose.Click -= [Your Event];
        else if (toDispose.GetType() == typeof(TabPage))
            toDispose.DoubleClick -= [Your Event];
        else if (toDispose.GetType() == typeof(Label))
            toDispose.MouseMove -= [Your Event];

        toDispose.Dispose();
    }

This is extremely crude and there’s probably a much better way of doing it, but unless someone can come up with it, this will have to do. Thanks for your help everyone. You might just have saved my entire project.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:59:01+00:00Added an answer on May 13, 2026 at 9:59 am

    You need to also clear out the reference.

    while(tabControlToClear.Controls.Count > 0)
    { 
        var tabPage = tabControlToClear.Controls[0];
        tabControlToClear.Controls.RemoveAt(0);
        tabPage.Dispose(); 
    
        // Clear out events.
    
        foreach (EventHandler subscriber in tabPage.Click.GetInvocationList())
        {
            tabPage.Click -= subscriber;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that does a large number of searches by querying remote
So, I have an application I'm making that can be simply described as a
I have a application that's making calls against some WCF services I have hosted
I have a Monodroid application that does many webrequests. I am making requests from
I'm making an application for the iPad. I have a view controll that has
I'm making a swing based application where I have a JList that periodically get's
I am making a new application in MongoDB, and I have found that the
I have a web application that creates XML feeds on the fly, depending on
I have an application written in Delphi 2006 that was working fine in Windows
I have a client application that creates an array of boolean arrays which I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.