尝试替换 Controls.Clear() 以避免内存泄漏不起作用

编程入门 行业动态 更新时间:2024-10-26 14:33:09
本文介绍了尝试替换 Controls.Clear() 以避免内存泄漏不起作用 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我替换了:

panel.Controls.Clear();

与:

清除(面板);

地点:

public static void Clear(Control ctrl){而 (ctrl.Controls.Count > 0) ctrl.Controls[0].Dispose();}

我收到以下错误:在 Application.Run(new Form1());System.ObjectDisposedException 未处理无法访问已处置的对象.对象名称:'标签'.

知道为什么会这样吗?

谢谢.

请参阅如何在不导致内存泄漏的情况下清除()控件

抱歉,我可能正在处理一些我只想从其父级中删除的内容.我会检查的.感谢您的回答.

解决方案

我可能处理了一些 Control 我稍后在代码中使用的.

I replaced:

panel.Controls.Clear();

with:

Clear(panel);

Where:

public static void Clear(Control ctrl)
{
    while (ctrl.Controls.Count > 0) ctrl.Controls[0].Dispose();
}

And I get the following error: at Application.Run(new Form1()); System.ObjectDisposedException was unhandled Cannot access a disposed object. Object name: 'Label'.

Any idea why that might be?

Thanks.

EDIT: See How to Clear() controls without causing a memory leak

EDIT: Sorry, I’m probably disposing of something which I just want to remove from its parent. I’ll check that. Thanks for the answers.

解决方案

I was probably disposing of some Controls I was using later on in the code.

这篇关于尝试替换 Controls.Clear() 以避免内存泄漏不起作用 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-29 00:01:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/744716.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   内存   以避免   Controls   Clear

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!