Thank you for your input!
Okay, that explains why it didn't give any problems earlier - similar constructs are used throughout the entire codebase, and a small testing program I wrote confirmed the destruction order.
The problem API call is in the following lines, in detail, it's the buildWindowClassName() call, together with CreateWindow():
WNDCLASSEX wc =
CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
&staticWndProc, 0, sizeof(MicrosoftWindow*), inst,
IconHandle, CursorHandle, BackdropBrush,
NULL, buildWindowClassName().getChars(), NULL };
LOG_AND_THROW_EXCEPTION("Failed to register window class");
0, 0, 0, 0,
NULL, NULL, inst, NULL);
LOG_AND_THROW_EXCEPTION("Failed to create window");
The latter exception is thrown, and GetLastError() returns 1407, which is the code for "Cannot find window class".
If I rewrite the code, as to first build the window class name once, and then save the result in a local variable, using the local variable in both calls, everything works out alright.
I already checked buildWindowClassName(), it always returns a new, identical string.
Perhaps Windows uses the string pointer, rather than the actual string contents, for naming the window class ? Both pointers are obviously different...
The local variable actually is a better solution in that case - but I'd still like to know where the old code went wrong.