Menu manipulation causes crashing
Posted on 2002-06-26
I have a menu that needs to change depending on the state of the application. At startup and at certain triggers thereafter, a function is called that modifies the menu by loading it fresh from the resources then removing items that are inapplicable.
The code is such:
void CMainFrame::changeMenu(MENU_STATE menuState)
HMENU hMenu = ::LoadMenu(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));
CMenu *menu = new CMenu();
//loop through the menu, examining the text of each top level menu item.
for(int i=0; i<menu->GetMenuItemCount(); i++)
menu->GetMenuString(i, menuText, MF_BYPOSITION);
if( (menuState == WORKING) || (menuState == OFFSITE) )
if( (!menuText.Compare("Solo")) ||
i--; // Make sure you don't skip the new item in a deleted index.
if( (menuState == WORKING_OFFSITE) &&
(!menuText.Compare("&Site Approved")) )
The document associated with the setMenuHandle() call has GetDefaultMenu() overridden to return the handle pointed to by a member variable, which is set with the setMenuHandle call.
The problem is that it crashes at different points throughout this function, the second time the function is hit each time the application is run. Actually sometimes it doesn't crash at all, but usually it does and never the first time through. When it crashes, even though I'm running it in debug it doesn't give me the debug info, but opens a different devstudio instead.
Always a different crash spot, and no obvious junk in any of the variables before a crash. Seems like a memory management thing to me, what am I doing wrong?