DynamoRIO / DynamoRIO/drmemory
DrMemory Crash with simple Win32 API program that creates OpenGL context
- Dominant language
- C
- Stars
- 2.7k
- Forks
- 290
- PR merge metrics
- No merged PRs in 30d
Description
There's an immediate crash when executing the following program in DrMemory.

This occured in both 2.3.0 and the latest build 2.3.18510 I just downloaded. This program is as minimal as I could get it from my much larger game. Previously, this same code had no issue running under DrMemory 2.2, however it suddenly stopped working producing the same error you see here under 2.3. There was definitely no major changes to the code base in this time, I made the minimal program to prove that to myself.
I suspect that this could be one of the newer major releases from Windows. This is running on Windows 10 Version 10.0.19041 Build 19041. The program is 32bit.
The program was build with GCC 10.2.0 released through MSYS2 packages. Compiler options
```-ggdb -Wall -std=c++2a -static-libstdc++ -static-libgcc -static```
**Does the problem go away when running in light mode (pass `-light` to Dr. Memory)?**
No
**Does the problem go away when running with the options `-leaks_only -no_count_leaks -no_track_allocs`?**
No
**Does the problem go away when running under plain DynamoRIO? Do this by running `dynamorio/bin32/drrun -- ` or `dynamorio/bin64/drrun -- ` depending on the bitwidth of your applicaiton. (Ignore warnings about "incomplete installation".)**
Yes, the program appears to execute correctly
**What happens with the debug version of Dr. Memory and of its underlying engine DynamoRIO? Try this by passing `-debug -dr_debug -pause_at_assert`. Are any messages reported?**
Program appears to execute.
```drmemory -debug -dr_debug -pause_at_assert bin/uitest.exe
~~Dr.M~~ Dr. Memory version 2.3.18510
~~Dr.M~~ Running "bin/uitest.exe"
~~Dr.M~~ Using system call file C:\Users\david\AppData\Roaming\Dr. Memory\symcache\syscalls_wow64.txt
~~Dr.M~~ WARNING: application exited with abnormal code 0xc0000005```
**Expected behavior**
To just exit after creating a window
The full code of the toy program that crashes:
```
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define OEMRESOURCE
#include
#include
#include
#include#include
#include
#include
#includeconst int WIN_WIDTH = 1280;
const int WIN_HEIGHT = 720;typedef std::int32_t GLint;
const std::uint32_t WGL_CONTEXT_CORE_PROFILE_BIT_ARB = 0x0001;
const std::uint32_t WGL_CONTEXT_DEBUG_BIT_ARB = 0x0001;
const std::uint32_t WGL_SAMPLE_BUFFERS_ARB = 0x2041;
const std::uint32_t WGL_SAMPLES_ARB = 0x2042;
const std::uint32_t WGL_CONTEXT_MAJOR_VERSION_ARB = 0x2091;
const std::uint32_t WGL_CONTEXT_MINOR_VERSION_ARB = 0x2092;
const std::uint32_t WGL_CONTEXT_FLAGS_ARB = 0x2094;
const std::uint32_t WGL_CONTEXT_PROFILE_MASK_ARB = 0x9126;typedef HGLRC (WINAPI* PFNWGLCREATECONTEXTATTRIBSARBPROC)
(HDC hDC, HGLRC hShareContext, const std::int32_t* attribList);class Window
{
public:bool Init( const std::uint_fast16_t width,
const std::uint_fast16_t height)
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);m_width = width;
m_height = height;WNDCLASSW wcDummy;
std::memset(&wcDummy,0,sizeof(wcDummy));
wcDummy.lpfnWndProc = DefWindowProcW;
wcDummy.hInstance = GetModuleHandle(0);
wcDummy.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wcDummy.lpszClassName = L"Dummy";
wcDummy.style = CS_OWNDC;if(!RegisterClassW(&wcDummy))
{
return false;
}HWND windowDummy = CreateWindowW(wcDummy.lpszClassName,
L"Title Window",
WS_DISABLED,
0, 0, 0, 0,
0, 0,
wcDummy.hInstance,
NULL);
if(windowDummy == NULL)
{
return false;
}PIXELFORMATDESCRIPTOR pfdDummy =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24,
8,
0, 0, 0, 0, 0, 0
};HDC dummyDrawingContext = GetDC(windowDummy);
INT pixelFormatDummy = ChoosePixelFormat(dummyDrawingContext, &pfdDummy);
SetPixelFormat(dummyDrawingContext, pixelFormatDummy, &pfdDummy);HGLRC dummyContext = wglCreateContext(dummyDrawingContext);
wglMakeCurrent(dummyDrawingContext, dummyContext);if(wglGetCurrentContext() == NULL)
return false;PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
wglDeleteContext(dummyContext);
DestroyWindow(windowDummy);WNDCLASSW wc;
std::memset(&wc,0,sizeof(wc));
wc.lpfnWndProc = Window::WndProc;
wc.hInstance = GetModuleHandle(0);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszClassName = L"Real";
wc.style = CS_OWNDC | CS_DBLCLKS;if(!RegisterClassW(&wc))
{
return false;
}RECT wr = {0, 0, width, height};
AdjustWindowRect(&wr, WS_CAPTION|WS_VISIBLE|WS_SYSMENU, FALSE);int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);m_windowHandle = CreateWindowW(wc.lpszClassName,
L"Title Window",
WS_CAPTION|WS_VISIBLE|WS_SYSMENU,
(screenWidth - width) / 2, (screenHeight - height) / 2, wr.right-wr.left, wr.bottom-wr.top,
0,0,wc.hInstance,this);
if(m_windowHandle == NULL)
{
return false;
}SetFocus(m_windowHandle);
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24,
8,
0, 0, 0, 0, 0, 0
};m_drawingContext = GetDC(m_windowHandle);
INT pixelFormat = ChoosePixelFormat(m_drawingContext, &pfd);
SetPixelFormat(m_drawingContext, pixelFormat, &pfd);const GLint attribList[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 3,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
0
};m_glRenderContext = wglCreateContextAttribsARB(m_drawingContext, 0, attribList);
if(m_glRenderContext == NULL)
{
return false;
}if(!wglMakeCurrent(m_drawingContext, m_glRenderContext))
{
return false;
}if(wglGetCurrentContext() == NULL)
return false;return true;
}private:
static LRESULT CALLBACK WndProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
Window* thisWindow = NULL;if(message == WM_NCCREATE)
{
thisWindow = static_cast(reinterpret_cast(lParam)->lpCreateParams);SetLastError(0);
if(!SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast(thisWindow)))
{
if(GetLastError() != 0)
{
return FALSE;
}
}
}
else
thisWindow = reinterpret_cast(GetWindowLongPtr(hWnd, GWLP_USERDATA));if(thisWindow)
return thisWindow->RealWndProc(hWnd,message,wParam,lParam);
return DefWindowProcW(hWnd,message,wParam,lParam);
}LRESULT CALLBACK RealWndProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch(message)
{
case WM_CLOSE:
{
wglDeleteContext(m_glRenderContext);Gdiplus::GdiplusShutdown(m_gdiplusToken);
DestroyWindow(hWnd);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
MSG msg = {0};
msg.hwnd = hWnd;
msg.message = message;
msg.wParam = wParam;
msg.lParam = lParam;
return DefWindowProcW(hWnd, message, wParam, lParam);
}
return 0;
}std::uint_fast16_t m_width;
std::uint_fast16_t m_height;
HWND m_windowHandle;
HGLRC m_glRenderContext;
HDC m_drawingContext;
ULONG_PTR m_gdiplusToken;
};int main()
{
try
{
Window window;
if(!window.Init(WIN_WIDTH,WIN_HEIGHT))
{
std::cerr << "ERROR: Couldn't init window" << std::endl;
return 0;
}
}
catch(const std::exception& e)
{
std::cerr << e.what() << std::endl;
}return 0;
}```
Contributor guide
Assessment
This issue has not been assessed yet.