Q224181: FIX: GDI Resouce Leak Using Checkboxes in ListVew Control

Article: Q224181
Product(s): Microsoft Visual Basic for Windows
Version(s): 6.0
Operating System(s): 
Keyword(s): kbCtrl kbListView kbResource kbVBp600bug kbVBp600fix kbGrpDSVB kbVS600sp4fix kbVS600sp5
Last Modified: 26-JUL-2001

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual Basic Learning Edition for Windows, version 6.0 
- Microsoft Visual Basic Professional Edition for Windows, version 6.0 
- Microsoft Visual Basic Enterprise Edition for Windows, version 6.0 
-------------------------------------------------------------------------------

SYMPTOMS
========

While loading and unloading forms that contain ListView controls using
Checkboxes, a large memory and GDI resource leak occurs.

CAUSE
=====

The image list for the ListView control's Checkboxes is not being destroyed when
the form is unloaded.

RESOLUTION
==========

This problem can be worked around by setting the Checkboxes property at run-time
rather than in the design environment. The Checkboxes property must be set to
True in the form's Activate event and then to False in the form's QueryUnload
event:

1. Open a new Standard EXE project. Form1 is generated by default.

2. From the Project menu, choose Components, select "Microsoft Windows Common
  Controls 6.0" and click OK.

3. Add a second form (Form2) to the project.

4. Add a ListView control (ListView1) to Form2. Leave the Checkboxes property
  for the control set to the default of False.

5. Add the following code to Form2's Activate Event:

  Private Sub Form_Activate()
      ' Set the ListView's checkboxes property to True.
      ListView1.Checkboxes = True
      
      ' Do any initialization of the ListView here.
      With ListView1
          .ListItems.Add , , "Item 1"
          .ListItems.Add , , "Item 2"
      End With 
  End Sub

6. Add the following code to Form2's QueryUnload event:

  Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
      ' Set the checkboxes property
      ' of the listview to False.
      ListView1.Checkboxes = False
  End Sub

7. Add a CommandButton (Command1) to Form1. Add the following code to Form1's
  code window:

  	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

8. Start a program to monitor system resources as follows:

   - Windows NT: Enter CTRL+ALT+DEL, and click Task Manager. Select the
     Performance tab.
   - Windows 95/98/Me: 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.

9. Run the project and click on the Command1 button on Form1. Notice the
  resource usage.

Result: Excessive resource use doesn't occur.

STATUS
======

Microsoft has confirmed this to be a bug in the Microsoft products listed at the
beginning of this article. This bug was corrected in the next service pack for
Visual Studio 6.0.

For additional information about Visual Studio service packs, click the article
numbers below to view the articles in the Microsoft Knowledge Base:

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

  Q194295 HOWTO: Tell That a Visual Studio Service Pack Is Installed

You can download the latest Visual Studio service pack from the following
Microsoft Web site:

  Visual Studio Product Updates
  (http://msdn.microsoft.com/vstudio/downloads/updates.asp)

MORE INFORMATION
================

Steps to Reproduce Behavior
---------------------------

1. Open a new Standard EXE project. Form1 is generated by default.

2. From the Project menu, choose Components, check "Microsoft Windows Common
  Controls 6.0," and click OK.

3. Add a second form (Form2) to the project.

4. Add a ListView control (ListView1) to Form2 and set its Checkboxes property
  to True.

5. Add a CommandButton control (Command1) to Form1 and paste the following code
  into Form1's code window:

  	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

6. Start a program to monitor system resources as follows:

   - Windows NT: Enter CTRL+ALT+DEL and click Task Manager. Select the
     Performance tab.
   - Windows 95/98/Me: 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.

7. Run the project. Click on the Command1 button on Form1. On NT, you should
  watch as the memory used by the process climbs continuously while running the
  program, and is only cleaned up when exiting Visual Basic or the compiled
  program.

Result: Excessive resource use.

Additional query words: sp4

======================================================================
Keywords          : kbCtrl kbListView kbResource kbVBp600bug kbVBp600fix kbGrpDSVB kbVS600sp4fix kbVS600sp5fix 
Technology        : kbVBSearch kbAudDeveloper kbZNotKeyword6 kbZNotKeyword2 kbVB600Search kbVB600
Version           : :6.0
Issue type        : kbbug
Solution Type     : kbfix

=============================================================================