Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CF 3.5内存泄漏-为什么组件被实例化,但从未调用components.Add(…)?_C#_Winforms_Memory Leaks_Compact Framework - Fatal编程技术网

C# CF 3.5内存泄漏-为什么组件被实例化,但从未调用components.Add(…)?

C# CF 3.5内存泄漏-为什么组件被实例化,但从未调用components.Add(…)?,c#,winforms,memory-leaks,compact-framework,C#,Winforms,Memory Leaks,Compact Framework,我注意到我的Compact Framework 3.5应用程序中存在内存泄漏,我想我已经找到了原因:这些组件从未添加到表单的IContainer components字段中。为什么VisualStudio2008没有添加每个组件,有没有办法解决这个问题 //Form overrides dispose to clean up the component list. [System.Diagnostics.DebuggerNonUserCode()] protected override void

我注意到我的Compact Framework 3.5应用程序中存在内存泄漏,我想我已经找到了原因:这些组件从未添加到表单的
IContainer components
字段中。为什么VisualStudio2008没有添加每个组件,有没有办法解决这个问题

//Form overrides dispose to clean up the component list.
[System.Diagnostics.DebuggerNonUserCode()]
protected override void Dispose(bool disposing)
{
    try {
        if (disposing && components != null) {
            // Components is disposed... but nothing was ever added to it!!!
            components.Dispose();

        }
    } finally {
        base.Dispose(disposing);
    }
}

//Required by the Windows Form Designer

private System.ComponentModel.IContainer components;
//NOTE: The following procedure is required by the Windows Form Designer
//It can be modified using the Windows Form Designer.  
//Do not modify it using the code editor.
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
    this.lblName = new System.Windows.Forms.Label();
    this.butNext = new System.Windows.Forms.Button();
    this.butPrev = new System.Windows.Forms.Button();
    this.butClose = new System.Windows.Forms.Button();
    this.butMore = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    //lblName
    //
    this.lblName.Font = new System.Drawing.Font("Tahoma", 12f, System.Drawing.FontStyle.Bold);
    this.lblName.Location = new System.Drawing.Point(6, 38);
    this.lblName.Name = "lblName";
    this.lblName.Size = new System.Drawing.Size(373, 22);
    this.lblName.Tag = "Estimated Speed ({0})";
    this.lblName.Text = "Test";

            // ... SNIP ...
    }

这不是内存泄漏。表单实际上没有
组件
字段-这只是在设计时实例化和使用的

您可能会想到表单的
控件
集合。你发布的代码自更新世以来一直是.NET的标准代码——你真的认为微软这些年会错过这么大的东西吗


如果确实存在内存泄漏(您是如何确定的?),几乎可以肯定,这是由于没有对所有需要调用
Dispose()
的对象调用
Dispose()
造成的。NET在垃圾收集方面做得很好,但不是100%。

控件是组件,但并非所有组件都是控件。这可能会提供一些有用的背景资料。“组件”容器用于存放任何必须处理的非控制组件

表单的控件应通过“控件”属性进行处置。在“剪报”下方的某个地方,您应该会看到类似的代码

this.Controls.Add(this.lblName);

您不使用任何组件,仅使用控件。他们被父母抛弃了。