Microsoft KB Archive/813354

= FIX: You may notice a memory leak when you use the DateTimePicker control in the .NET Framework 1.0 =

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:

http://www.microsoft.com/downloads/details.aspx?familyid=6978D761-4A92-4106-A9BC-83E78D4ABC5B&displaylang=en



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



Steps to reproduce the behavior
 Create a Microsoft Windows application. To do this, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. The New Project dialog box appears. Under Project Types, click Visual C# Projects. Under Templates, click Windows Application.</li> In the Name box, type MyProject, and then click OK. By default, a form that is named Form1 is created.</li> On the View menu, click Toolbox.</li> 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.</li> Add two Button controls to the Form1 form. By default, the button1 Button control and the button2 Button control are created.</li>  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 =&quot;Before test: &quot; + before.ToString; The GC.GetTotalMemory method retrieves the number of bytes currently allocated in the managed memory. </li>  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 = &quot; After test: &quot; + after.ToString; </li>  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 = &quot; After Garbage Collection: &quot; + after.ToString; The GC.Collect method forces the garbage collection and tries to collect all the inaccessible and dereferenced memory. </li> On the File menu, click Save All to save all the files.</li></ol> </li> Build and then run the Windows application. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> On the Build menu, click Build Solution.</li> 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.</li> 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.</li> Click button2.</li></ol>

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.</li></ol>

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

<div class="references_section">