views:

104

answers:

2

I am interested in allocating pointers, storing those in the LPARAM data of a comboboxex control, and making that control responsible for deleting those pointers when it is destroyed.

Since I am working in MFC, I can subclass a CComboBoxEx, and add either a message handler or a virtual member function.

The question is: Is this pattern possible with Win32 / MFC?

Basically, how does a control get notified that its corresponding HWND is being destroyed?

The documentation for WM_DESTROY:

The WM_DESTROY message is sent when a window is being destroyed. It is sent to the window procedure of the window being destroyed after the window is removed from the screen. (Emphasis Mine)

Unfortunately, my vague recollection is that this means that OnDestroy() is too late for handling anything that requires interacting with the associated HWND, no?

Can I query the elements in the comboboxex during OnDestroy()? Is there another hook I can use that occurs "Just before my window / control is destroyed (instead of after!)?"

I wonder if I overrode CBEM_DELETEITEM for my subclass and forced it to delete the LPARAM data. Are all items explicitly deleted when a comboboxex is destroyed? If so, are they all destroyed via that message (does the control send that message to itself?)

+2  A: 

During OnDestroy() your window will still be valid -- if it weren't, your window wouldn't have gotten the message at all, since it's sent via the standard Windows messaging system.

You are on the right track -- this type of scenario is what OnDestroy() is for.

mwigdahl
Yup, just because the window is removed from the screen (as Mordachai emphasized is the case with WM_DESTROY) doesn't mean the HWND is no longer valid.
Nick Meyer
Thanks - Indeed, hooking the WM_DESTROY does indeed work fine.
Mordachai
+2  A: 

From MSDN:

OnDestroy is called first for the CWnd being destroyed, then for the child windows of CWnd as they are destroyed. It can be assumed that all child windows still exist while OnDestroy runs.

Nikola Smiljanić