I am maintaining an old C++ application using MFC and recently encounter weird freezing in a part that has been working since at least 2018... I traced the freezing to this code:
void CVisBeregning::process_messages()
{
MSG msg;
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (vb->GetSafeHwnd()) {
vb->UpdateWindow();
}
}
specifically to the UpdateWindow() method.
There, CVisBeregning is a class derived from CDialog and vb is its member defined as
static CVisBeregning * vb;
vb is set to be equal to this in the class constructor. (I need a global access to the (only) object of that class, since it is used in callbacks from an external code.)
The freeze happens when the first if returns false, i.e., when there were no messages to process.
It therefore looks like I can avoid it by moving the second if-block inside the first one, but I would first like to understand why the freeze happens.
In my particular test case, the process_messages() method is called more than 10 times before it freezes, but in on those occasions the first if is true.
In other words, it seems to crash the first time the if is false.
More info:
The if (vb->GetSafeHwnd()) check is needed, as the code runs in two modes and in one of them the class does not have any window attached (so the UpdateWindow() is never called).
I am using Visual Studio 2019 on Windows 10 x64.