@hansw2000 @Nerixyz
The bug doesn’t happen in 32-bit mode when linked by lld. And as you already know, it never happens when linked by link.exe
Interestingly though, I think that I found the offending piece of code:
#if SLOW_1
#define Assert(Expression) if(!(Expression)) { *(int *)0 = 0; }
#else
#define Assert(Expression)
#endif
This is little hack that I use to just pause the debugger (essentially). But the fix? Oh you might not guess it: I did try undefining my SLOW_1 macro to just see if it work (and it does!!!), but I took it a step further, and kept my SLOW_1 macro, and just commented out THIS ONE SINGLE LINE:
LRESULT CALLBACK
Win32MainWindowProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam)
{
switch (Message)
{
case WM_SIZE:
{} break;
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_KEYDOWN:
case WM_KEYUP:
{
Assert(!1); /////// <<<<<<<<<<<<< THIS LINE
} break;
// ... closing statements
}
What the above code does, is that it checks if somehow windows ends up in it’s procedure function instead of passing through my own keyboard logic (which is why I want it to crash).
Finally: Reproduction Steps
// somefile.h
#if PROJECT_SLOW
#define Assert(Expression) if(!(Expression)) { *(int *)0 = 0; }
#else
#define Assert(Expression)
#endif
And in win32_main.cpp file:
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <stdio.h>
#include "somefile.h"
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
Assert(!0); ///////////////////////////// Offending Iine
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
///////// Standard Win32 stuff /////////
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
Somehow, in debug builds, Windows ends up in CRTStartup function even if the condition of my Assert would never make it do a null dereference.
edit: forgot to add the command-line:
clang-cl ..\src\win32_main.cpp user32.lib gdi32.lib /DPROJECT_SLOW /Z7 /Od /MT -fuse-ld=lld
All code is compiled in x64