Question

My program use a modeless dialog to interact with the user and also has a tray icon. A user can quit the app immediately by using the tray icon.

BOOL OnInitDialog()
{
    init data...
}

void OnDestroy()
{
    destroy data...
}

void OnSomeButton()
{
    CFileDialog dlg;
    ...
    dlg.DoModal(m_hWnd));
    access data...
    ...
}

void OnMenuExit()
{
    DestroyWindow();
}

The problem is that when I popup a modal dialog(OnSomeButton), and then quit using the tray icon menu, the main dialog is destroyed first, and then the modal one returns, trying to access some invalid data, causing a crash.

I know i can add some check code before accessing the data, but is there any other way? Is there any way to ensure OnSomeButton() returns before window destroy?

Was it helpful?

Solution

Yeah. When you quit from the tray menu, you can send a WM_CLOSE or similar message to your modal dialog that causes it to exit. Even if your main window is destroyed before that OnSomeButton returns you will be okay provided the remainder of that function does not access any class internals (member variables etc). You could ensure this by having the window proc of your modal dialog return an 'abort' code or something when it is closed in this way.

OTHER TIPS

You need to add your own application level code. There is no system support for this issue primarily because there can be so many pecularities that no generic approach is possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top