• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

destroying a modeless dialog

If I put "delete this" in the PostNcDestroy function, do I still need to do other stuff in the OnOK and OnCancel functions for a modeless dialog, or does the "delete this" in the PostNcDestroy function take care of everything?
1 Solution
Vinayak KumbarSr Program ManagerCommented:

I dont think we have to do any more clean up as PostNcDestroy() and OnNcDestroy() functions are the last member functions that are called. Those functions will do the default cleanup. But if U have used some pointers and allocated memory, then U have to delete(or free ) the memory.

For further information about those functions U can look into the OnLine help of those functions(U might have already seen that!)

delete this; will delete the this pointer and object. But if u have allocated some memory as I said, then it will not be deleted. That memory should be deleted by U in the code explicitly.
delete this will call your dialog's destructor. If you have any allocated memory you must delete it also (VinExpert right).

But the problem in that PostNCDestroy will not be called from OnOk() and OnCancel() handlers if your dialog is modeless.

Thus you need call DestroyWindow() within OnOk() and OnCancel() handlers. This will bring up the call to PostNCDestroy() , delete this (in PostNCDestroy()) will be called, then your dialog's destructor will make all the rest clean-up (if neccesary)

Hope this helps
i do this in CYourDlg::OnDestroy()
and it work fine. OnDestroy() called after OnOK/OnCancel.

but i recommand that you post a message to a frame window (who create the dialog) and put the object point as LPARAM parameter. the frame delete the object when it recevie the message.
sample code:


in the frame window
LONG CYourFrame::OnYourMessage(WPARAM wP,LPARAM lP)
  delete ((CYourDlg*)lP);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now