FIX: Memory Leak Occurs After Form is Unloaded

ID: Q174289


The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0


SYMPTOMS

A memory leak occurs in a Visual Basic project that has forms that are loaded and unloaded several times.


CAUSE

The small icon of a form remains in memory after the form is unloaded, which results in a memory leak.


RESOLUTION

To work around this bug in Visual Basic 4.0, use the DestroyIcon function during the Form_Unload event to remove the small icon from memory. Copy the following code sample to the code window of each form in your project:


   Private Declare Function SendMessage Lib "user32" _
      Alias "SendMessageA" _
      (ByVal hwnd As Long, _
       ByVal wMsg As Long, _
       ByVal wParam As Long, _
       lParam As Long) As Long

   Private Declare Function DestroyIcon Lib "user32" _
      (ByVal hIcon As Long) As Long

   Private Const WM_SETICON = &H80
   Private Const MiniIcon = 0
   Private Const NullIcon = 0

   Private Sub Form_Unload(Cancel As Integer)
      Dim hIcon As Long
      hIcon = SendMessage(hwnd, WM_SETICON, MiniIcon, ByVal _
      NullIcon)
      If hIcon <> 0 Then
         DestroyIcon hIcon
      End If
   End Sub 


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been fixed in Visual Basic Version 5.0.


MORE INFORMATION

Programs that load and unload forms frequently can cause a significant leak in memory because the operating system will lose 1148 bytes every time a form is unloaded. The workaround is to destroy the icon when the form is unloaded.

Use the SendMessage function with the WM_SETICON message to get the handle of either the large or small icon and to then replace it with the icon specified by the NullIcon parameter; in this case replacing it with Null. With the MiniIcon parameter set to zero, the message applies to the small icon.

The return value is the handle to the old icon which is removed from memory by the DestroyIcon function.

Steps to Reproduce the Behavior

  1. Start a new project. Form1 is created by default.


  2. Add another Form to the project.


  3. Add the following code to the Click event of Form1:
    
          Dim i as Integer
          For i = 1 To 1000
             Form2.Show
             Form2.Hide
          Next i
     


  4. Press the F5 key to run the project. You will notice a performance drop where Form2 loads more slowly as the iterations progress. Press the CTRL+BREAK keys to end the application.


Keywords : kbVBp400 kbVBp500 kbGrpVB VB4WIN VBKBObj VBKBVB kb32bitOnly
Version : WINDOWS:4.0 5.0
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: January 5, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.