尝试替换 Controls.Clear() 以避免内存泄漏不起作用 - 为什么? [英] Trying to replace Controls.Clear() to avoid memory leak doesn’t work – why?

查看:17
本文介绍了尝试替换 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆