Visual C++使用 Visual C++ 开发的项目2005 - 在 Visual C 中2010,调试断言在菜单打开时失败,但发布模式有效,如何修复?
我刚刚开始处理一个项目,该项目是由其他人使用 MFC 在 Visual C++ 2005 中开发的。为此,我安装了 Visual C++ 2010 - 我没有使用这两个版本(或就此而言,根本没有 Visual Studio)的经验。
该应用程序有一个使用CMenu
类实现的菜单栏,并从资源初始化(即使用菜单编辑器创建)。
如果我在调试模式下运行应用程序,则只要打开菜单栏中的任何菜单,以下调试断言就会失败:
文件名:afxwin1.inl
断言(具有适当的上下文):
_AFXWIN_INLINE HMENU CMenu::GetSafeHmenu() const
{ ASSERT(this == NULL || m_hMenu == NULL || ::IsMenu(m_hMenu));
return this == NULL ? NULL : m_hMenu; }
根据调试器, this
设置为指向我的菜单的指针,并且 this->m_hMenu
也设置为指针 - 但是,调试器似乎对类型感到困惑,值为0xdeadbeef {未使用=??? }
(当然,对于更无聊的 0xdeadbeef
值)。
看起来有些东西被破坏了,而且很可能在项目代码中 - 我知道这一点。但是,如果我创建一个发布版本,它运行时不会出现错误,并且菜单会正确显示。此外,我使用 Visual C++ 2010 中的菜单编辑器编辑了菜单,并保存了它 - 没有任何变化。因此,我排除了资源格式错误导致的菜单兼容性问题。
我在 ThinkPad T61p 上运行 Windows XP SP3。
创建菜单的代码是
CMenu menu;
menu.LoadMenu(RESOURCE_NAME);
SetMenu(&menu);
并在 CFrameWnd
子类的上下文中运行。
我在谷歌上搜索了很多,并且相当肯定以前没有人遇到过这个问题;另外,我对 C++、MFC 和 Visual Studio 都是新手。非常感谢您的帮助;如果我可以提供更多问题元数据,请告诉我,我会这样做。 提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 MSDN (http://msdn.microsoft.com/en-us/library/177ay1x0.aspx)
CMenu::LoadMenu() 需要传入一个参数,但示例代码中缺少该参数。
请检查并确保您传递的是有效的菜单资源 ID。
编辑:
您的菜单对象是在堆栈上构造的,一旦超出范围就会被销毁。
您从哪里调用 CWnd::SetMenu ?您需要确保对象的生命周期可以持续到下一个 CWnd::SetMenu ,否则您将持有/引用一个悬空指针。
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.