MFC menu issues
-
I have a simple menu written out, it has 5 options on the main menu bar, and under the 4th menu, I have 3 menu items. I'm trying to check or uncheck the last item under the 4th menu option, it is identified as ID_ACTION_FIT. Here is the code I have so far to check the item:
CMenu * MainMenu = GetMenu(); CMenu * ActionMenu = MainMenu->GetSubMenu(3); ActionMenu->CheckMenuItem (ID_ACTION_FIT, MF_BYCOMMAND);
That code is inside the event handler for the item I'm trying to check. I have done this before once with no problems but it's not working now. When it reaches the second line quoted and attempts to get the submenu, it fails a debug assertion. I don't have the code for GetSubMenu so I don't know what is actually being tested. The MainMenu pointer is valid. I don't know what's wrong but I think I have an error that's too obvious to see. Does anyone have an idea of what is the problem? -
I have a simple menu written out, it has 5 options on the main menu bar, and under the 4th menu, I have 3 menu items. I'm trying to check or uncheck the last item under the 4th menu option, it is identified as ID_ACTION_FIT. Here is the code I have so far to check the item:
CMenu * MainMenu = GetMenu(); CMenu * ActionMenu = MainMenu->GetSubMenu(3); ActionMenu->CheckMenuItem (ID_ACTION_FIT, MF_BYCOMMAND);
That code is inside the event handler for the item I'm trying to check. I have done this before once with no problems but it's not working now. When it reaches the second line quoted and attempts to get the submenu, it fails a debug assertion. I don't have the code for GetSubMenu so I don't know what is actually being tested. The MainMenu pointer is valid. I don't know what's wrong but I think I have an error that's too obvious to see. Does anyone have an idea of what is the problem? -
All it says is:
Debug assertion failed!
Program: C:\...path\to\program.exe
File: f:\vs70builds\9955\vc\MFCATL\ship\atlmfc\include\afxwin1.inl
Line: 875For information on assertions...
(Press Retry to debug the application
[Abort] [Retry] [Ignore]
If I press Retry, it tells me Unhandled exception at some address. If I press Break, and go to the call stack and double click the CMenu::GetSubMenu() item on top of the stack, I get a box that says "There is no source code available for the current location." and I can see the disassembly if I want. I never deleted any implementation files but if I do have the source code for that function, VS .NET cannot find it and I don't know how to tell it where it is.
-
I added a "MainMenu->AssertValid();" after I get the main menu pointer but before I get the submenu. I get another assertion but this time I got the source code for that function. The assertion line is "ASSERT(m_hMenu == NULL || ::IsMenu(m_hNull);". m_hNull looks valid, on this particular run of the program, it is 0x0000e900 which is not NULL. I don't see how this assertion is triggered, nothing about the menu data looks suspicious.
-
I have a simple menu written out, it has 5 options on the main menu bar, and under the 4th menu, I have 3 menu items. I'm trying to check or uncheck the last item under the 4th menu option, it is identified as ID_ACTION_FIT. Here is the code I have so far to check the item:
CMenu * MainMenu = GetMenu(); CMenu * ActionMenu = MainMenu->GetSubMenu(3); ActionMenu->CheckMenuItem (ID_ACTION_FIT, MF_BYCOMMAND);
That code is inside the event handler for the item I'm trying to check. I have done this before once with no problems but it's not working now. When it reaches the second line quoted and attempts to get the submenu, it fails a debug assertion. I don't have the code for GetSubMenu so I don't know what is actually being tested. The MainMenu pointer is valid. I don't know what's wrong but I think I have an error that's too obvious to see. Does anyone have an idea of what is the problem?The assertion in
CMenu::GetSubMenu
isASSERT(::IsMenu(m_hMenu));
, so the main menu handle seems to be invalid. Where (in which class and function) do you call this code? Regards, BB http://spin.bartoszbien.com -
The assertion in
CMenu::GetSubMenu
isASSERT(::IsMenu(m_hMenu));
, so the main menu handle seems to be invalid. Where (in which class and function) do you call this code? Regards, BB http://spin.bartoszbien.comI'm calling it in my CChildView class, in the event handler for the menu item I'm trying to toggle. When you said that, I realized that the CChildView only corresponds to the view itself, not the entire app, at least as far high as the menu exists. I changed the code to get the menu like this:
CWnd * pParent = ::AfxGetMainWnd(); CMenu * MainMenu = pParent->GetMenu(); CMenu * ActionMenu = MainMenu->GetSubMenu(3);
Which looks like it's picking up the right things when debugging, I won't be sure it's right until I write more code, but thanks for helping me point that out. -
I have a simple menu written out, it has 5 options on the main menu bar, and under the 4th menu, I have 3 menu items. I'm trying to check or uncheck the last item under the 4th menu option, it is identified as ID_ACTION_FIT. Here is the code I have so far to check the item:
CMenu * MainMenu = GetMenu(); CMenu * ActionMenu = MainMenu->GetSubMenu(3); ActionMenu->CheckMenuItem (ID_ACTION_FIT, MF_BYCOMMAND);
That code is inside the event handler for the item I'm trying to check. I have done this before once with no problems but it's not working now. When it reaches the second line quoted and attempts to get the submenu, it fails a debug assertion. I don't have the code for GetSubMenu so I don't know what is actually being tested. The MainMenu pointer is valid. I don't know what's wrong but I think I have an error that's too obvious to see. Does anyone have an idea of what is the problem?According to CMenu's documentation, "[t]he return value is undefined if CWnd is a child window". Try changing the first line to:
CMenu* MainMenu = AfxGetMainWnd()->GetMenu();
[UPDATE]: After posting this I noticed you had already found the problem. Sorry. -- jlr http://jlamas.blogspot.com/[^] -- modified at 0:36 Thursday 6th October, 2005