简体   繁体   English

当我不处理我的MDI子窗体时,代码分析投诉“ CA2000在失去作用域之前先处理对象”

[英]Code Analysis Complains about “CA2000 Dispose objects before losing scope” when I Don't Dispose My MDI Child Form

CA2000 Dispose objects before losing scope CA2000在失去作用域之前处置对象

In method FormMain.barButtonItem1_ItemClick(object, ItemClickEventArgs) 在方法FormMain.barButtonItem1_ItemClick(object, ItemClickEventArgs)

Call System.IDisposable.Dispose on object 'frm' before all references to it are out of scope. 在对对象'frm'的所有引用超出范围之前,请调用System.IDisposable.Dispose。 Winpro FormMain.cs 32 Winpro FormMain.cs 32

Method : 方法 :

private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
{
    FormCustomerList frm = new FormCustomerList();
    frm.MdiParent = this;
    frm.Show();
}

This is not a serious problem, but why is this fired? 这不是一个严重的问题,但是为什么会这样呢?

I can't use finally frm.Dispose() or using() because form will not be shown. 我最终不能使用frm.Dispose()using()因为将不会显示表单。

I have also tried to handle form closing and then dispose but violation is always here. 我也曾尝试处理表单关闭然后进行处理,但违规始终在这里。

Code Analysis can't tell that frm is still doing anything after it exits scope. 代码分析无法判断frm退出作用域后仍在执行任何操作。 In this specific case, the object needs to stay alive after the function is done with it. 在这种特定情况下,完成功能后,对象需要保持活动状态。

The "correct" way to handle this is to maintain a reference to frm in the parent form. 处理此问题的“正确”方法是在父表单中维护对frm的引用。 This reference can then be disposed in the Dispose() method of the parent form. 然后可以将该引用放在父窗体的Dispose()方法中。

private FormCustomerList frm;
private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
{
    frm = new FormCustomerList();
    frm.MdiParent = this;
    frm.Show();
}

If you have multiple sub forms that can be created (which is likely if you're using MDI), you can maintain a List<> of child forms. 如果您可以创建多个子表单(如果使用的是MDI,则可能会出现这种情况),则可以维护子表单的List<>

private List<FormCustomerList> frms = new List<FormCustomerList>();
private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
{
    FormCustomerList frm = new FormCustomerList();
    frms.Add(frm);
    frm.MdiParent = this;
    frm.Show();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 控制器上的“ CA2000在失去作用域之前先放置对象” - “CA2000 Dispose objects before losing scope” on controller “ CA2000丢失范围之前处置对象”原因不明 - “CA2000 Dispose objects before losing scope ” for unknown reason 我是否需要在这两个XmlDataSource对象上都调用.Dispose()? “ CA2000在失去作用域之前先处理对象”告诉我.Dispose() - Do I need to call .Dispose() on both of these XmlDataSource objects? “CA2000 Dispose objects before losing scope” tells me to .Dispose() it FxCop 10.0找不到CA2000“在丢失范围之前处置对象” - FxCop 10.0 can't find CA2000 “Dispose objects before losing scope” C#CA2000在丢失范围之前处置对象 - C# CA2000 Dispose Object Before Losing Scope CA2000在失去DirectoryEntry对象的MVC项目中的作用域之前,先处理对象 - CA2000 Dispose objects before losing scope in MVC project for DirectoryEntry object C#CA2000:使用FileStream / XmlTextReader在丢失范围之前处置对象 - C# CA2000:Dispose objects before losing scope using FileStream/XmlTextReader 基于不适用于 System.Web.UI.Control 对象的 CA2000“在失去范围之前处理对象”创建自定义 FXCop 规则 - Creating a Custom FXCop Rule based on CA2000 “Dispose Objects Before Losing Scope” that doesn't apply to System.Web.UI.Control objects “ CA2000:在失去作用域之前处理对象”,构建Unity容器 - “CA2000: Dispose object before losing scope” building Unity container 为什么此方法导致代码分析错误CA2000:调用Dispose() - Why does this method cause Code Analysis error CA2000: Call Dispose()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM