Heap corruption when using CreateWindowExW

I have some problems with heap corruption. A warning can be observed when using the CreateWindowExW function. I know that this is usually a memory error, but how can I find it in such a situation? There are no new variables before calling CreateWindowExW, and I cannot enter this function. Here is the code.

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;} 

In this line

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

there is a window with a warning message

Windows called a breakpoint in the drm.exe file. This may be due to heap corruption, which indicates an error in drm.exe or any of the downloaded dll files. It can also be triggered by pressing the F12 button while drm.exe has focus. The output window may have more diagnostic information.

I click "Continue" and it shows

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

However, CreateWindowExW returns a nonzero value, and the window is created as it should ...

0
source share
2 answers

As mentioned above, heap damage is often detected after real corruption has already occurred with some DLLs / modules loaded into your process. From your post, it seems that this problem is related to the Windows platform, so I suggest you use WinDBG / Pageheap and find out where the actual memory corruption occurs. One very good article about analyzing corruption in the memory heap can be found in the book "Advanced Windows Debugging by By: Mario Hewardt, Daniel Pravat" Chapter 06

http://advancedwindowsdebugging.com/ch06.pdf

+4
source

Change

 WNDCLASSEX wcex = { sizeof(WNDCLASSEX) }; 

to

 WNDCLASSEX wcex = { 0 }; 

You initialize the elements of the WNDCLASSEX pointer to non-zero (but meaningless values, namely sizeof (WNDCLASSEX)).

-2
source

Source: https://habr.com/ru/post/972103/


All Articles