I'm having trouble understand an exception thrown (read access violation, this is nullptr) when closing my application. The exception occurs on GetDlgItem(IDC_Button1)->EnableWindow(true);
when the CDialogEx::OnCancel();
is called. It appears as though the thread is correctly shut down but the error still persists.
When c_DialogFunctionsThreadRunning = false;
is called before the MessageBox
then the issue does not occur but this also means the thread is terminated before the prompt is accepted or cancelled.
void CFRP_3D_PrinterDlg::OnBnClickedShutdown()
{
if (MessageBox(_T("Close program?"), _T("Program"), MB_ICONQUESTION | MB_OKCANCEL | MB_TOPMOST) == IDOK)
{
c_DialogFunctionsThreadRunning = false;
StateMachine.StateEnter(ShutDown);
CDialogEx::OnCancel();
}
}
void CFRP_3D_PrinterDlg::DialogFunctionsThread()
{
c_DialogFunctionsThreadRunning = true;
CWinThread *CDialogFunctionsThread = AfxBeginThread(DoDialogFunctions, this, THREAD_PRIORITY_NORMAL, 0, 0, nullptr);
CDialogFunctionsThread->m_bAutoDelete = true;
}
UINT CFRP_3D_PrinterDlg::DoDialogFunctions(LPVOID t)
{
CFRP_3D_PrinterDlg *DialogFunctions = static_cast<CFRP_3D_PrinterDlg *>(t);
DialogFunctions->DoDialogFunctions();
return NULL;
}
void CFRP_3D_PrinterDlg::DoDialogFunctions()
{
while (c_DialogFunctionsThreadRunning && c_DialogCreated)
{
GetDlgItem(IDC_Button1)->EnableWindow(true);
Sleep(20);
}
}