What you have here looks like a race condition. A modal QDialog runs its own event loop, so your application behaves like a multithreaded application and you need to take care of concurrency and race conditions.
When you receive a second in your main event loop, you call CMsgBox::CloseDlg()
and CMsgBox::showMsgBox()
in quick succession. However, CloseDlg()
tells the dialog's event loop to return, but CloseDlg()
actually returns before the dialog's event loop is done cleaning up, and showMsgBox()
attempts to call exec()
on a dialog which hasn't finished exiting yet.
What you need to do is, when you call CMsgBox::CloseDlg()
, connect to the finished(int)
signal, and only when you receive the finished(int)
can you safely exec()
the dialog again.
NOTE: When connecting to the finished(int)
signal, make sure to use a Qt::QueuedConnection
instead of a Qt::DirectConnection
which is the default.