Reputation: 15
I have a question seems simple but confuse me. I didn't get answer from Google. In Visual Studio MFC project, I test this sample code:
void CEmptySingleDocApp::OnAppAbout()
{
TmpVarAboutDlg();
}
void TmpVarAboutDlg() {
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
The aboutDlg in the sample code is a local variable, but why the code runs well? My blind guess is that when create a CAboutDlg, you are using a resource, which is in this line:
CAboutDlg::CAboutDlg() noexcept : CDialogEx(IDD_ABOUTBOX)
Do I guess right?
Upvotes: 0
Views: 430
Reputation: 51496
aboutDlg
is indeed a local variable that is destroyed when it goes out of scope at the end of TmpVarAboutDlg
. This is not a problem as the code calls DoModal. DoModal
only returns after the dialog has been dismissed, and its corresponding C++ object is no longer needed.
While the code does use an application-provided resource to construct the dialog, the same principles hold if the dialog were created from an in-memory dialog template, or dynamically through code.
Note that while a program is blocked on a modal dialog, the system still dispatches messages for other windows. For example, if you move the dialog, the system will generate WM_PAINT
messages for windows underneath it as appropriate.
Upvotes: 2