2

I'm battling the old "Error creating window handle" fun. My application has a center area that hosts whatever control the user is currently working on. When the user loads a new control, I destroy the old one. Or at least I try to. When I run the task manager, I see in horror as the number of GDI objects is not reducing as a new control is getting loaded. This is what I'm doing to (try to) kill off a contorl I'm done with. The counter shows a huge number in the end, which seems about right, but the GDI objects in Task Manager stays about the same, until it hits 10,000, and then everything comes crashing down.

    private void RecursivelyKillYourself(Control C) {
        if (C.Controls != null && C.Controls.Count > 0) {
            List<Control> Controls = C.Controls.OfType<Control>().ToList();
            C.Controls.Clear();
            Controls.ForEach(c_inner => RecursivelyKillYourself(c_inner));
        } else {
            C.Dispose();
            num++;
        }
    }

EDIT

Fixed - gotta make sure you create tooltips correctly. Also, as a commentator pointed out, that long silly Recursive method is unnecessary. Just a simple Dispose will do.

bad

(new SMcMaster.TabOrderManager(this)).SetTabOrder(SMcMaster.TabOrderManager.TabScheme.AcrossFirst);

good

ToolTip T = new ToolTip();
T.SetToolTip(btnAddPropertyInvoice, "Add Properties");
components.Add(T);
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • 1
    Isn't it sufficient to just call `Dispose` on the control that you're working with? The control should dispose of its children automatically. Assuming, of course, that the control was properly written. – Jim Mischel Nov 18 '10 at 15:40
  • If you have custom controls and are overriding Dispose or better yet not implementing needed additional disposing you need to do within those controls, you could find yourself in this predicament. Have you verified the actual behavior of the Dispose within the custom control types...if they do exist...? Good article on Dispose...http://blogs.msdn.com/b/jfoscoding/archive/2005/08/12/450835.aspx – Aaron McIver Nov 18 '10 at 15:40
  • I was creating my tooltips incorrectly, which were not getting disposed - see edit. Thanks for your responses – Adam Rackis Nov 18 '10 at 15:56
  • Aaron, throw that link up as an answer so I can accept it. It's from that that I got the idea to clean up my tooltips. – Adam Rackis Nov 18 '10 at 16:07

1 Answers1

1

Verify you are addressing Dispose properly.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88