Microsoft KB Archive/813354

From BetaArchive Wiki

Article ID: 813354

Article Last Modified on 5/12/2007



APPLIES TO

  • Microsoft .NET Framework 1.0




SYMPTOMS

If you create an application by using the Microsoft .NET Framework version 1.0, and you use the DateTimePicker control in the application, you may notice a memory leak when you run the application.

CAUSE

This problem occurs because the garbage collector does not collect the inaccessible memory. The DateTimePicker control is associated with the Microsoft.Win32.UserPreferenceChangedEventHandler delegate. When you dispose of an object of the DateTimePicker control, this delegate is not dereferenced. Therefore, this delegate prevents the garbage collector from collecting the DateTimePicker control object.

RESOLUTION

To resolve this problem, obtain the latest service pack for the .NET Framework 1.0.

To obtain the latest service pack, visit the following Microsoft Web site:

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section. This problem was first corrected in the Microsoft .NET Framework 1.0 Service Pack 3.

MORE INFORMATION

Steps to reproduce the behavior

  1. Create a Microsoft Windows application. To do this, follow these steps:
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project. The New Project dialog box appears.
    3. Under Project Types, click Visual C# Projects.
    4. Under Templates, click Windows Application.
    5. In the Name box, type MyProject, and then click OK. By default, a form that is named Form1 is created.
    6. On the View menu, click Toolbox.
    7. Add three TextBox controls to the Form1 form. By default, the textBox1 TextBox control, the textBox2 TextBox control, and the textBox3 TextBox control are created.
    8. Add two Button controls to the Form1 form. By default, the button1 Button control and the button2 Button control are created.
    9. Add the following code to the Form1_Load event handler of the Form1 form.

      GC.Collect();
      GC.WaitForPendingFinalizers();
      long before = GC.GetTotalMemory(true);
      textBox1.Text ="Before test: " + before.ToString();

      The GC.GetTotalMemory method retrieves the number of bytes currently allocated in the managed memory.

    10. On the Form1 form, double-click button1, and then add the following code to the button1_Click event handler of the Form1 form.

      long after;
      for (int i=0; i<100; i++) 
      {
      DateTimePicker dtp=new DateTimePicker();    
      dtp.Dispose();
      dtp=null;
      }
      after = GC.GetTotalMemory(true);
      textBox2.Text = " After test: " + after.ToString();
    11. On the Form1 form, double-click button2, and then add the following code to the button2_Click event handler in the Form1 form.

      GC.Collect();
      GC.WaitForPendingFinalizers();
      long after = GC.GetTotalMemory(true);
      textBox2.Text = " After Garbage Collection: " + after.ToString();

      The GC.Collect method forces the garbage collection and tries to collect all the inaccessible and dereferenced memory.

    12. On the File menu, click Save All to save all the files.
  2. Build and then run the Windows application. To do this, follow these steps:
    1. On the Build menu, click Build Solution.
    2. On the Debug menu, click Start. The Form1 dialog box appears. The number of bytes that are allocated in managed memory appears in the textBox1 text box.
    3. Click button1.

      The application creates an instance of the DateTimePicker control at run time, and then disposes of the DateTimePicker control. This behavior occurs 100 times. The number of bytes in managed memory after 100 occurrences appears in the textBox2 text box. The garbage collector does not collect the inaccessible memory locations.
    4. Click button2.


    The number of bytes that remain in managed memory after the application calls the garbage collector appear in the textBox2 text box. You may notice that most of the inaccessible memory is still in managed memory.

For additional information, click the following article number To view the article In the Microsoft Knowledge Base:

824684 Description of the standard terminology that Is used To describe Microsoft software updates


REFERENCES

For more information about the UserPreferenceChangedEventHandler delegate, visit the following Microsoft Developer Network (MSDN) Web site:

For more information about the DateTimePicker class, visit the following MSDN Web site:


Additional query words: DateTimePicker control memory leak

Keywords: kbhotfixserver kbqfe kbmemory kbgarbagecollect kbforms kbcode kbbug kbfix kbqfe kbnetframe100presp3fix KB813354