Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

valid window handle

Posted on 2006-04-20
11
Medium Priority
?
489 Views
Last Modified: 2013-11-20
Hi,

In my MDI app, I'm keeping a vector of dialog handles so I can reference these windows later on, something like:

void OnBnCreateDialogWindow()
{
    CMyDialog *pNewDlg = new CMyDlg();
    pNewDlg->Create(...);
    theApp.m_vDialogHandles.push_back(pNewDlg->m_hWnd);
}

Anytime I close one of these dialogs, then do a count of valid hwnds in the vector, the count never seems to decrease:

void OnBnCountValidHwnds()
{
    int n = 0;
    for (int i = 0; i < theApp.m_vDialogHandles.size(); i++)  {
        if (::IsWindow(theApp.m_vDialogHandles[i])) {
            n++;
        }
    }

    CString str;
    str.Format("there are [%i] valid handles.", n);
    AfxMessageBox(str);
}

Now if I opened a few dialogs, close them all, then run the count function, it thinks each of the window handles are still valid. Any ideas? Any better ways to do this?

Thank you

0
Comment
Question by:minnirok
  • 6
  • 3
  • 2
11 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16498627
All you have posted here is how you add handles to the collection.  How do you remove them from the collection?


There is a virtual function PostNcDestroy - that is a place often used for tidying up.  Do you remove the handle from the collection there for instance?
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16498650
The dialogs are modeless - how do you destroy them is also relevant  (Hopefully NOT with CDialog::OnOK or CDialog::OnCancel).
0
 
LVL 7

Author Comment

by:minnirok
ID: 16498675
Ah ok, I guess since I am allocating a new dialog object with 'new', 'delete' is never being called on it, even though the WM_CLOSE message is getting called, I guess the memory for it is still valid even though you can't 'see' the dialog anymore. Is it ok to then do something like:

void CMyDialog::OnClose()
{
    CDialog::OnDestroy();
    delete this;
}

then the hwnd will not be valid anymore?

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:minnirok
ID: 16498702
Hi Andy,

Sorry didn't see your posts before my last one - yes I am not deleting the dialogs anyplace. So should I call:

    delete this

in that PostNcDestroy() message handler? Will it be called even when the user clicks the close icon on the titlebar?

Thanks
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16498703
You may try using IsWindowVisible() API function OR you may manually assign NULL to window handle in list after closing window.

MAHESH
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16498806
I use code like this for 'disposable' windows to remove memory allocated with new.

void CXYZDlg::PostNcDestroy()
{
      CDialog::PostNcDestroy();
      delete this;
}
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16498884
void CMyDialog::OnClose()
{
HWND hWnd = GetSafeHwnd();    //Get the window handle, now remove from the collection
    CDialog::OnDestroy();
}
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16498927
ps.
I had a quick look at PostNcDestroy - at that point the window has been destroyed so no valid handle at that point, the OnClose should be better for tidying up the HANDLE in the collection, use the PostNcDestroy for the memory.
0
 
LVL 7

Author Comment

by:minnirok
ID: 16499285
Hi Andy,

How is your PostNcDestroy handler getting called? Right now I have:

void CMyDialog::OnClose()
{
     OnClose();
     delete this;
}

void CMyDialog::PostNcDestroy()
{
    AfxMessageBox("Post Destroy");
    CDialog::PostNcDestroy();
}

My message is never getting called in PostNcDestroy(), not when I close the dialog, nor when I exit the application. I added it through the vc 2003 wizard,

Thanks
   
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 16500244
void CMyDialog::OnClose()
{
     OnClose();
     delete this;
}

At this point you have deleted the 'this' - so PostNcDestroy isn't being called.

void CMyDialog::PostNcDestroy()
{
    AfxMessageBox("Post Destroy");
    CDialog::PostNcDestroy();
}


do this
void CMyDialog::OnClose()
{
//remove handle from collection
     DestroyWindow();
}

void CMyDialog::PostNcDestroy()
{
    AfxMessageBox("Post Destroy");
    CDialog::PostNcDestroy();
delete this;
}
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16500296
In some cases PostNcDestroy does not get called automatically, so we have to call it ourselves.

e.g
void CMyDialog::OnDestroy()
{
    CDialog::OnDestroy();    
    ....
    PostNcDestroy ();        
}

MAHESH
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question