FIX: GDI Resource Leak Using Checkboxes in Treeview Control

ID: Q228859


The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0


SYMPTOMS

When a form containing a Treeview control(s) that uses checkboxes is loaded and unloaded, a significant memory and GDI resource leak occurs.


CAUSE

The GDI resources used by the checkboxes on the controls are not freed when the form is unloaded.


RESOLUTION

Only load each form once and keep the form loaded. Hide the form when it is not in use and show the form when it is needed.


STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.This bug was corrected in Visual Studio 6.0 Service Pack 3. For more information about Visual Studio service packs, please see the following articles in the Microsoft Knowledge Base:

Q194022 INFO: Visual Studio 6.0 Service Packs, What, Where, Why

Q194295 HOWTO: Tell That Visual Studio 6.0 Service Packs Are Installed


MORE INFORMATION

Steps to Reproduce Behavior

  1. Open a new Visual Basic Standard EXE project. Form1 is created by default.


  2. Add a Command Button to Form1.


  3. Add the following code to the General Declarations section of Form1:


  4. 
    Private Sub Command1_Click()
       Dim f As Form2
       Dim i As Long
       For i = 0 To 100
          Set f = New Form2
          f.Show
          Unload f
          DoEvents
          Set f = Nothing
       Next i
    End Sub 
  5. On the Project menu, click Add Form to add a second form to the project. Form2 is created by default.


  6. On the Project menu, click Components. Check Microsoft Windows Common Controls 6.0 and click OK.


  7. Add a Treeview control to Form2.


  8. Add the following code to the General Declarations section of Form2:


  9. 
    Private Sub Form_Load()
       TreeView1.Checkboxes = True
       TreeView1.Nodes.Add , , , "Item1"
       TreeView1.Nodes.Add , , , "Item2"
       TreeView1.Nodes.Add , , , "Item3"
    End Sub 
  10. Start a program to monitor system resources as follows:


    • Windows NT: Enter CTRL+ALT+DEL and click the Task List button. Select the Monitor tab.


    • Windows 95/98: Start the System Monitor tool, which is located in the System Tools folder in the Accessories folder. (If the System Monitor tool is not available, it can be installed from the Control Panel using the Add/Remove Programs option.) On the Edit menu, click Add Item. Click the Memory Manager Category, click the Allocated Memory Item, and click OK.


  11. Run the project. Click the Command1 button on Form1. Notice the resource usage. Most of the time, resources fall below the critical level on the first run and, if not, they should on the second run.

    Result: Excessive resource use.


Additional query words:

Keywords : kbservicepack kbCmnCtrls kbCtrl kbTreeView kbVBp600bug kbGrpVB kbVS600sp2 kbVS600SP1 kbVS600sp3fix
Version : WINDOWS:6.0
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: May 19, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.