If your program crashes on ((MainDlg*)GetParent())->a;
, it most likely means GetParent() returned NULL, indicating failure. Check for return values before using the result of GetParent().
MainDlg *p = (MainDlg *)GetParent();
if(p == NULL) {
int err = GetLastError(); // <--- inspect this return value, it'll tell you why it failed
}
GetParent() usually fails for one of the following reasons:
- Window wasn't created yet, either by directly or indirectly calling Create()
- The window is a top-level window that is unowned or does not have the WS_POPUP style
- The owner window has WS_POPUP style.
Since you seems to be working with dialogs(your cast to MainDlg), I suspect #1 applies and you're calling GetParent() prior to dialog creation/OnInitDialog.
GetLastError()
GetParent()