title: how to hook control closing in an MFC custom control class
body: <p>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.</p>
  
  <p>Since I am working in MFC, I can subclass a CComboBoxEx, and add either a message handler or a virtual member function.</p>
  
  <p>The question is: Is this pattern possible with Win32 / MFC?</p>
  
  <p>Basically, how does a control get notified that its corresponding HWND is being destroyed?</p>
  
  <p>The documentation for WM_DESTROY:</p>
  
  <blockquote>
    <p>The WM_DESTROY message is sent when a window is being destroyed. It is sent to the window procedure of the window being destroyed <strong>after the window is removed from the screen</strong>. <em>(Emphasis Mine)</em></p>
  </blockquote>
  
  <p>Unfortunately, my vague recollection is that this means that OnDestroy() is too late for handling anything that requires interacting with the associated HWND, no?</p>
  
  <p>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!)?"</p>
  
  <p>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?)</p>