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 t开发者_StackOverflow中文版he 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);
Verify you are addressing Dispose properly.
精彩评论