FIX: Memory Leak Occurs After Form is UnloadedLast reviewed: September 26, 1997Article ID: Q174289 |
The information in this article applies to:
SYMPTOMSA memory leak occurs in a Visual Basic project that has forms that are loaded and unloaded several times.
CAUSEThe small icon of a form remains in memory after the form is unloaded, which results in a memory leak.
RESOLUTIONTo 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 STATUSMicrosoft 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 INFORMATIONPrograms 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
Keywords : vb432 VB4WIN vb5all VBKBObj VBKBVB Version : WINDOWS:4.0 5.0 Platform : WINDOWS Issue type : kbbug Solution Type : kbfix |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |