I just started working on a project that was developed in Visual C++ 2005 using MFC, by someone else. To do so, I installed Visual C++ 2010 – I have no experience using either version (or visual studio at all, for that matter).
The application has a menu bar that is implemented using the CMenu class, and is initialized from a resource (ie, is created using the menu editor).
If I run the application in debug mode, the following debug assertion fails as soon as I open any menu in the menu bar:
Filename: afxwin1.inl
Assertion (with appropriate context):
_AFXWIN_INLINE HMENU CMenu::GetSafeHmenu() const
{ ASSERT(this == NULL || m_hMenu == NULL || ::IsMenu(m_hMenu));
return this == NULL ? NULL : m_hMenu; }
According to the debugger, this is set to a pointer to my menu, and this->m_hMenu is set to a pointer as well – however, the debugger seems to be confused about the type, the value is 0xdeadbeef {unused=??? } (for a more boring value of 0xdeadbeef, of course).
It would seem that something is broken, and most likely in the project code – I am aware of this. However, if I create a release build, it runs without errors, and the menu is displayed correctly. Moreover, I edited the menu using the menu editor in Visual C++ 2010, and saved it – there was no change. I’m therefore ruling out menu compatibility issues from the resource being in the wrong format.
I am running Windows XP SP3 on a ThinkPad T61p.
The code that creates the menu is
CMenu menu;
menu.LoadMenu(RESOURCE_NAME);
SetMenu(&menu);
And runs in the context of a subclass of CFrameWnd.
I have searched google a lot and am fairly certain that nobody had this problem before; Also, I’m a newbie to all of C++, MFC and Visual Studio. Help would be immensely appreciated; If I can provide more problem metadata, please tell me and I will do so.
Thanks in advance!
According to MSDN (http://msdn.microsoft.com/en-us/library/177ay1x0.aspx)
CMenu::LoadMenu() requires a parameter to be passed in, which is missing in your sample code.
Do check on that and make sure you are passing a valid menu resource id.
Edit:
Your menu object is constructed on the stack and will be destroyed once it went out of scope.
Where are you calling your CWnd::SetMenu from ? You need to make sure the object lifespan can last until the next CWnd::SetMenu else you will be holding/referring a dangling pointer.