c++ - 使用 CreateWindowExW 时堆损坏

标签 c++ visual-studio-2010 heap-corruption

我在堆损坏方面遇到了一些问题。使用 CreateWindowExW 函数时可以观察到警告。我知道这通常是内存错误,但在这种情况下我该如何查找呢?在调用 CreateWindowExW 之前没有新变量,我无法进入该函数。这是代码。

HWND GetMainWnd(HINSTANCE hInstance){
static HWND hWnd = NULL;
if (hWnd)
    return hWnd;

RETURN_AT_ERROR(hInstance, NULL);

WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style          = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = MainWndProc;
wcex.hInstance      = hInstance;
wcex.hCursor        = ::LoadCursorW(NULL, IDC_ARROW);
wcex.lpszClassName  = g_config->GetWndClass();

ATOM atom = ::RegisterClassExW(&wcex);
RETURN_AT_ERROR(atom != 0, NULL);

hWnd = ::CreateWindowExW(WS_EX_LEFT, g_config->GetWndClass(), 0, WS_POPUP | WS_MINIMIZEBOX | WS_CLIPCHILDREN, 0, 0, 0, 0, 0, 0, hInstance, 0);

return hWnd;}

在这个字符串上

hWnd = ::CreateWindowExW(WS_EX_LEFT, g_config->GetWndClass(), 0, WS_POPUP | WS_MINIMIZEBOX | WS_CLIPCHILDREN, 0, 0, 0, 0, 0, 0, hInstance, 0);

有一个警告消息框

Windows has triggered a breakpoint in drm.exe. This may be due to a corruption of the heap, which indicates a bug in drm.exe or any of the DLLs it has loaded. This may also be due to the user pressing F12 while drm.exe has focus. The output window may have more diagnostic information.

我按“继续”,它显示

Unhandled exception at 0x77dae753 in app.exe: 0xC0000374: A heap has been corrupted.

但是 CreateWindowExW 返回一个非零值并且窗口按应有的方式创建...

最佳答案

正如上面所指出的,堆损坏通常是在您的进程中加载​​的某些 DLL/模块已经发生真正的损坏之后才检测到的。从你的帖子来看,这个问题似乎是特定于 Windows 平台的,所以我建议你使用 WinDBG/Pageheap 并找出实际内存损坏发生的位置。一篇关于堆内存损坏分析的非常非常好的文章可以从《高级Windows调试》一书中找到,作者:作者:Mario Hewardt;Daniel Pravat第06章

http://advancedwindowsdebugging.com/ch06.pdf

关于c++ - 使用 CreateWindowExW 时堆损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22072847/

相关文章:

visual-studio-2010 - Visual Studio 折叠快捷方式 "not currently available"

c++ - 在用 C++ 编写这个对象数组时,我做错了什么?

C++11 - enable_if - 类定义之外的函数实现

c++ - 您将如何编写程序来简化方程式?

debugging - 如何在调试后自动关闭调试器打开的文件?

c# - 序列化和反序列化 Visual Studio 解决方案文件 - 还是以编程方式编辑?

c++ - 删除动态数组后堆损坏

c++ - 自定义 vector 类中的堆损坏错误

c++ - 在新 [] : with or without user destructor 之后删除

c++ - 深度+颜色的3D投影