Window handle creation error

I am struggling with the old "Window handle creation error". My application has a center area that hosts any control the user is working on. When the user loads a new control, I destroy the old one. Or at least I am trying. When I start the Task Manager I see in horror as the number of GDI objects does not decrease when a new control is loaded. This is what I am doing (trying to) kill with contorl which I ended up with. The counter shows a huge amount at the end, which seems correct, but the GDI objects in the task manager stay about the same until it hits 10,000 and then everything crashes.

    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 - be sure to create tooltips correctly. Also, as the commenter pointed out, this long silly recursive method is unnecessary. Just a simple Dispose will do.

badly

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

      

well

ToolTip T = new ToolTip();
T.SetToolTip(btnAddPropertyInvoice, "Add Properties");
components.Add(T);

      

+2


source to share


1 answer


Make sure you are handling Dispose correctly.



+1


source







All Articles