从 FlowLayoutPanel 清除控件不调用析构函数?

发布于 2024-08-03 09:45:13 字数 180 浏览 12 评论 0原文

抱歉,如果我遗漏了一些明显的内容,但我正在尝试从 FlowLayoutPanel - (panelName).Controls.Clear(); 中清除控件(一系列用户控件)。不幸的是,这似乎并没有调用面板上对象的析构函数 - 任务管理器中的“用户对象”列不断上升,直到达到 10,000 并抛出异常。

有谁知道我在这里缺少什么?

Sorry if I'm missing something obvious, but I'm trying to clear the controls (a series of user controls) from a FlowLayoutPanel - (panelName).Controls.Clear();. Unfortunately this doesn't seem to be calling the destructors for the objects on the panel - the User Objects column in the task manager just keeps going up and up, until it hits 10,000 and throws an excecption.

Does anyone know what I'm missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终止放荡 2024-08-10 09:45:13

不是一个解决方案,而是一个解决方法 - 对象似乎确实被这个(粗略的,从内存中)代码破坏了:

while(FlowLayoutPanel.Controls.Count > 0)
     FlowLayoutPanel.Controls.Remove(0);

Not a solution, but a workaround - the objects do seem to be destroyed by this (rough, from memory) code:

while(FlowLayoutPanel.Controls.Count > 0)
     FlowLayoutPanel.Controls.Remove(0);
爱给你人给你 2024-08-10 09:45:13

eftpotrm 的上述解决方法仍然使我的用户句柄计数不断增长,但是,如果您在删除控件后手动处置,那么对我来说就可以 100% 修复它。

while (myFlowLayoutPanel.Controls.Count > 0)
{
     var controltoremove = myFlowLayoutPanel.Controls[0];
     myFlowLayoutPanel.Controls.Remove(controltoremove);
     controltoremove.Dispose();
}

eftpotrm's workaround above still kept the user handles count growing for me, however, if you just manually dispose after removing the control, that fixed it 100% for me.

while (myFlowLayoutPanel.Controls.Count > 0)
{
     var controltoremove = myFlowLayoutPanel.Controls[0];
     myFlowLayoutPanel.Controls.Remove(controltoremove);
     controltoremove.Dispose();
}
昔日梦未散 2024-08-10 09:45:13

.NET 没有析构函数的概念。 .NET 有一个叫做“终结器”的东西,它在语法上看起来像 C# 中的析构函数。有关更多信息,请查看 Jeff Richter 关于 CLR 如何工作的精彩书籍 - CLR via C#

您可能希望对象实现 IDisposable 模式,然后在使用完它们后调用它们的 Dispose() 方法。

.NET does not have the concept of destructors. .NET has something called "finalizers" which look syntactically like destructors in C#. For more information, check out Jeff Richter's awesome book on how the CLR works -- CLR via C#.

You may want the objects to implement the IDisposable pattern, and then call their Dispose() method when you're done with them.

温柔女人霸气范 2024-08-10 09:45:13

尝试使用内存分析器,(例如ants) 它会告诉你是什么让控件保持活动状态。尝试第二次猜测此类问题非常困难。

Red-gate 提供了 14 天的尾巴,这应该有足够的时间来解决这个问题并确定内存分析器是否为您提供长期价值。

市场上还有很多其他内存分析器(例如.NET Memory Profiler)其中有免费试用版,但是我发现 Red-Gate 工具很容易使用,所以倾向于先尝试它们。

Try using a memory profiler, (e.g. ants) it will tell you what is keeping the control alive. Trying to 2nd guess this type of problem is very hard.

Red-gate gives 14 days tail that should be more then enough time to tack down this problem and decide if a memory profiler provides you with long term value.

There are lots of other memory profilers on the market (e.g. .NET Memory Profiler) most of them have free trials, however I have found that the Red-Gate tools are easy to use, so tend try them first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文