title: default WM_DESTROY not properly cleaning up child windows
body: <p>I have a WTL 8.0 SDI application for Windows Mobile 5. In this contrived example below, I create a view, destroy it, then re-create it. But, when it's re-created assertions in the WM_INITDIALOG handler fail because the control's HWND isn't valid.</p>
  
  <p>I note that I can fix this by handling WM_DESTROY in CMyView and manually destroying every child control. But, I didn't think I should have to. <a href="http://msdn.microsoft.com/en-us/library/aa922178.aspx" rel="nofollow noreferrer">MSDN even says</a>:</p>
  
  <blockquote>
    <p>This message is sent first to the
    window being destroyed and then to the
    child windows (if any) as they are
    destroyed.</p>
  </blockquote>
  
  <p>Anybody have an idea as to what's going on?</p>
  
  <p>Edit: If I handle <a href="http://msdn.microsoft.com/en-us/library/ms632636(VS.85).aspx" rel="nofollow noreferrer">WM_NCDESTROY</a> in CMyView, all of the the child control handles are still valid! (<code>some_control_.IsWindow()==TRUE</code>) That's not how it's supposed to be...</p>
  
  <p>Thanks,
  PaulH</p>
  
  <pre><code>class CMyView : public CDialogImpl&lt; CMyView &gt;,
                  public CWinDataExchange&lt; CMyView &gt;
  {
      // &lt;snip&gt; Message Map and other standard WTL macros &lt;/snip&gt;
  
      LRESULT OnInitDialog( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL&amp; /*bHandled*/ )
      {
          DoDataExchange( FALSE );
          // assertion fails within the SetWindowText() call
          // atlwin.h line 876
          // ATLASSERT(::IsWindow(m_hWnd));
          some_control_.SetWindowText( _T( "Foo" ) );
          return 0;
      };
  
  private:
      CEdit some_control_;
  }; // class CMyView
  
  class CMainFrame : public CFrameWindowImpl&lt; CMainFrame &gt;, 
                     public CUpdateUI&lt; CMainFrame &gt;,
                     public CMessageFilter, 
                     public CIdleHandler
  {
  public:
      // &lt;snip&gt; Message Map and other standard WTL macros &lt;/snip&gt;
  
      BOOL CMainFrame::PreTranslateMessage( MSG* pMsg )
      {
          if( CFrameWindowImpl&lt; CMainFrame &gt;::PreTranslateMessage( pMsg ) )
              return TRUE;
  
          return my_view_.PreTranslateMessage( pMsg );
      };
  
      LRESULT OnCreate( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL&amp; /*bHandled*/ )
      {
          CMessageLoop* pLoop = _Module.GetMessageLoop();
          ATLASSERT( pLoop != NULL );
          pLoop-&gt;AddMessageFilter( this );
          pLoop-&gt;AddIdleHandler( this );
  
          m_hWndClient = my_view_.Create( m_hWnd );
          my_view_.DestroyWindow();
          m_hWndClient = my_view_.Create( m_hWnd );
      };
  
  private:
      CMyView my_view_;
  }; // class CMainFrame
  </code></pre>