0
votes

I'm looking for a method or message that is always fired when an MFC Dialog (CDialog or CDialogEx) is getting destroyed.

I found the following messages which seem cause a dialogs destruction:

  • WM_DESTROY
  • WM_CLOSE
  • WM_QUIT (not only for dialogs but to quit from the main message loop which causes destruction as well)

Together with those inheritable function which get called when a dialog closes but not always:

  • OnOk()
  • OnCancel()

And I'm unsure about OnFinalRelease() as it seems to be something for OLE controls.

Is there a message or method always called or fired when a dialog gets destructed? I need to do some final heap cleanup on the destruction of my dialog instance.

2
Why a message? You are using a C++ wrapper class, use the destructor. - Hans Passant
I did that before and ended up having access violations because GDI+ was shut down at the point where destructors are being called. Many destructors of GDI+ objects use GDI+ calls in their destructors. Hence I can't delete a resource that was using GDI+ there. I need something that happens before the destruction of the wrapper class. - Vinz
It looks like you are not familiar with MFC Dialogs. They are not really designed for clean ups and when using heap together with GDI+ the one or the other attachment to dialog destroying methods are necessary. Anyw. I found an answer in the answers below. Thanks for your comments - Vinz

2 Answers

1
votes

CDialog::OnClose should be called before all the window objects gets destroyed, while CDialog::OnDestroy should be called once they are already destroyed (its opposite to CDialog::OnCreate). To make sure this is how it works in you case you can use Spy++ to see what messages are comming.

Instead of relying on windows messages, I would suggest to wrap whatever resoures you are using into some RAII (Resource Acquisition Is Initialization) idiom. This might be a std::unique_ptr or some specialized class. The will protect you against uncaught exceptions or missing windows messages.

1
votes

Override CDialogEx::PostNcDestroy (inherited from CWnd). This is always called by the framework after it is done destroying the window. CWnd::PostNcDestroy