Thread: Error when using GetParent()

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    Error when using GetParent()

    Hi everybody,
    I stumbled on a little error that is confusing.
    My program uses tabs to control two dialog boxes each having their own classes and functions. I instantiate them as follows:
    Code:
    if(GetCurSel() == 0) {
     if (theDialog1 != NULL) {
      theDialog1->SetFocus();
     }
     else {
      theDialog1 = new dialog1;
      theDialog1->Create(IDD_DIALOG1,this);
     }
    I also have some code that listens to messages from the dialog box and sets theDialog1 pointer appropriately:
    Code:
    ON_MESSAGE(WM_USER_DIALOG_DESTROYED, OnDialogDestroyed)
    
    LRESULT tabControl::OnDialogDestroyed (WPARAM wParam, LPARAM lParam)
    {
    	theDialog1 = NULL;
    	return 0;
    }
    In my Dialog1 class, I have this code for sending the message to the parent that instantiated an instance of this class when someone clicks on the ESC key:
    Code:
    // dialog1 message handlers
    void dialog1::PostNcDestroy()
    { 
    	CDialog::PostNcDestroy();
    	CWnd* pWindow = GetParent();
    	//AfxGetMainWnd()->
    	pWindow->SendMessage (WM_USER_DIALOG_DESTROYED, 0, 0);
    	delete this;
    }
    
    void dialog1::OnCancel()
    {
    	DestroyWindow();
    }
    Alas everytime the program reaches the line CWnd* pWindow = GetParent();
    it crashes and gives me a debug assertion failed error.
    Can anybody look at the code and let me know what I am doing wrong. Also if I use AfxGetMainWnd()-> it works fine but the class that instantiated the dialog window does not seem to get the message properly. I am thinking it's because it is not the main window. Can someone confirm this. Thanks
    Amish

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    got it

    I figured out that I was not passing the parent window when constructing the dialog1 instance. After that it works fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parent of a BrowseForFolder dialog box
    By @nthony in forum Windows Programming
    Replies: 4
    Last Post: 01-08-2007, 02:54 PM