如何清除()控制,而不会造成内存泄漏 [英] How to Clear() controls without causing a memory leak
问题描述
阅读后Control.Clear();
导致内存泄漏(在的 MSDN ),我在想,如果:
After reading about Control.Clear();
causing memory leaks (verified at MSDN), I was wondering if:
while (Controls.Count > 0) Controls[0].Dispose();
就足够了,或者我必须通过所有的控制控件中递归迭代?
will be enough, or do I have to iterate recursively through all controls within controls?
此外,没有任何理由做了 Control.Clear();
之后呢? (因为我看到有人的地方说法)?
Also, is there any reason to do a Control.Clear();
after that? (as I saw someone saying somewhere)?
感谢。
推荐答案
张贴的片段是正确的。没有明确的()是必需的,在Control.Dispose()方法已删除从控件集合的控制。 。这就是为什么它的工作原理
The posted snippet is correct. No Clear() is required, the Control.Dispose() method already removes the control from the Controls collection. Which is why it works.
循环的那么令人震惊的版本是:
The less alarming version of the loop is:
for (int ix = Controls.Count-1; ix >= 0; --ix) Controls[ix].Dispose();
没有必要通过控制孩子迭代,并处理掉,已经自动发生。
No need to iterate through the children of the control and dispose them, that already happens automatically.
这篇关于如何清除()控制,而不会造成内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!