2011-03-29 6 views
0

Compact Framework 3.5 응용 프로그램에서 메모리 누수가 발생하여 구성 요소가 내 양식의 IContainer components 필드에 추가되지 않는 이유를 발견했다고 생각합니다. Visual Studio 2008이 각 구성 요소를 추가하지 않는 이유는 무엇입니까?이를 해결할 방법이 있습니까?CF 3.5 메모리 누수 - 구성 요소가 인스턴스화 된 이유는 무엇입니까?하지만 구성 요소가 추가 호출되지 않았습니다 (...)?

//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 ... 
    } 
+1

구성 요소 만 사용하고 있으며 컨트롤 만 사용하고 있습니다. 그들은 부모님이 처분합니다. –

답변

1

이것은 메모리 누출이 아닙니다. 양식에 실제로 components 필드가 없습니다.이 필드는 디자인시에만 인스턴스화되고 사용됩니다.

대신 양식의 Controls 컬렉션을 생각할 수도 있습니다. 당신이 게시 한 코드는 홍적기 이후로 .NET에서 표준으로 사용되었습니다. 마이크로 소프트가이 모든 것을 그리워 할 것이라고 정말로 생각하십니까?

메모리 누수가있는 경우 (이것을 어떻게 결정 했습니까?) Dispose()이 호출되어야하는 모든 객체에서 Dispose()을 호출하지 않은 결과가 거의 확실합니다. .NET은 가비지 콜렉션을 훌륭하게 처리하지만 100 %는 아닙니다.

+0

+1 지질 참조! – ctacke

1

컨트롤은 구성 요소이지만 일부 구성 요소가 컨트롤이 아닙니다. 이 article은 유용한 배경을 제공 할 수 있습니다. '구성 요소'IContainer는 처리해야하는 모든 비 제어 구성 요소를 보유합니다.

양식의 컨트롤은 '컨트롤'속성을 통해 처리해야합니다. 'snip'아래 코드와 비슷한 코드가 표시되어야합니다.

this.Controls.Add(this.lblName); 
관련 문제