Article ID: 239943
Article Last Modified on 8/7/2007
APPLIES TO
- Microsoft Visual Basic 5.0 Learning Edition
- Microsoft Visual Basic 6.0 Learning Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Visual Basic 6.0 Enterprise Edition
This article was previously published under Q239943
SYMPTOMS
When a usercontrol has any top level menus where the NegotiateMenus property is non-zero, resources drain slowly when the focus is set to and from the usercontrol. The problem occurs in both the Visual Basic Design Environment (IDE) and as an EXE.
CAUSE
Visual Basic destroys the UserControl's main menu, but does not destroy all of the submenus under the main menu when the UserControl loses input focus.
STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in the latest service pack for Visual Studio 6.0.
For additional information about Visual Studio service packs, click the following article numbers to view the articles in the Microsoft Knowledge Base:
194022 INFO: Visual Studio 6.0 Service Packs, What, Where, Why
194295 HOWTO: Tell That a Visual Studio Service Pack Is Installed
To download the latest Visual Studio service pack, visit the following Microsoft Web site:
MORE INFORMATION
Steps to Reproduce and Work Around the Problem
- Start a new Visual Basic Standard EXE project. Form1 is created by default. The NegotiateMenus property of Form1 is set to True by default.
- On the Project menu, click Add User Control to add UserControl1 to the project.
- Add a CommandButton and a CheckBox control to UserControl1.
- Create a menu with several items and sub items on UserControl1. Set the NegotiatePosition property of each top level menu item to 1-Left.
Add the following code to the General Declarations section of UserControl1:
Option Explicit ' ' ' Demonstrates how to free menu resources orphaned by a UserControl. ' Zero-based array and one-based counter ' of cached UserControl submenus Private m_ahMenus() As Long Private m_nMenus As Integer Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, _ ByVal nPos As Long) As Long Private Declare Function GetMenuItemCount Lib "user32" _ (ByVal hMenu As Long) As Long Private Declare Function IsMenu Lib "user32" (ByVal hMenu As Long) As Long Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) _ As Long Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Private Sub UserControl_EnterFocus() ' the UserControl's main menu has been created and has ' replaced the form's main menu before any control on the ' UserControl gains input focus. If Check1.Value And (m_nMenus = 0) Then Call LoadMenus(GetMenu(GetParent(UserControl.hwnd))) ' Form1.hwnd)) Debug.Print "UserControl submenus loaded: " & m_nMenus End If End Sub Private Sub UserControl_ExitFocus() ' the UserControl's main menu has been destroyed, and has ' replaced been by the form's main menu. *But* all submenus ' under UserControl's main menu have not been destroyed. If m_nMenus Then Debug.Print "UserControl submenus destroyed: " & DestroyMenus End If End Sub Private Sub UserControl_Initialize() Check1.Caption = "Workaround" BorderStyle = 1 End Sub Private Sub Check1_Click() If (Check1.Value = vbChecked) And (m_nMenus = 0) Then Call LoadMenus(GetMenu(GetParent(UserControl.hwnd))) Debug.Print "UserControl submenus loaded: " & m_nMenus ElseIf (Check1.Value = vbUnchecked) And m_nMenus Then Erase m_ahMenus ' Clear the module level variables m_nMenus = 0 Debug.Print "No UserControl submenus will be destroyed" End If End Sub ' Pass the UserControl's top level menu on first call Private Sub LoadMenus(hMenu As Long) Dim nItems As Integer Dim i As Integer Dim hSubmenu As Long nItems = GetMenuItemCount(hMenu) For i = 0 To nItems - 1 hSubmenu = GetSubMenu(hMenu, i) If IsMenu(hSubmenu) Then ReDim Preserve m_ahMenus(m_nMenus) m_ahMenus(m_nMenus) = hSubmenu m_nMenus = m_nMenus + 1 End If Call LoadMenus(hSubmenu) ' recurse through sub menu Next End Sub Private Function DestroyMenus() As Integer Dim i As Integer Dim n As Integer For i = m_nMenus - 1 To 0 Step -1 If IsMenu(m_ahMenus(i)) Then ' Will destroy all submenus under the current submenu, toggle the ' For expression comments above to destroy individual submenus. Call DestroyMenu(m_ahMenus(i)) n = n + 1 End If Next ' Clear the mod level variables Erase m_ahMenus m_nMenus = 0 DestroyMenus = n End Function
- Close all of UserControl1's open windows.
- Add a CommandButton and a UserControl1 to Form1. UserControl11 is created by default.
Add the following code to the General Declarations section of Form1:
Private Sub Form_Click() Dim i As Integer For i = 1 To 2000 UserControl11.SetFocus DoEvents ' IMPORTANT: Allows SetFocus to complete before proceeding Command1.SetFocus DoEvents Me.Caption = i Next i End Sub
- Save your project.
- On Windows 95, Windows 98, or Windows Me, start the Resource Meter. The Windows NT or Windows 2000 Task Manager does not provide the same level of functionality.
- Run the project, and click on the form. Under Win9x, the Resource Meter shows a steady decline in User and System resources.
- Click the form again. On most systems, you observe problems with menu-related functionality such as Copy, Paste, and the Start menu. You may also see changes to fonts and icons used by the system.
- Click the Stop button. You may receive an Out of Memory dialog, which you can dismiss. Terminate the Visual Basic IDE and observe that the resources are released.
- Restart the Visual Basic IDE. Reload and run your project.
- Select the checkbox labeled Workaround and click the form. The Resource Meter shows no decline in resources.
Additional query words: sp4
Keywords: kbapi kbbug kbctrl kbfix kbmenu kbvs600sp4fix kbvs600sp5fix KB239943