protected:
     // common part of all constructors
     void Init();
+    // common part of wxDialog/wxFrame destructors
+    void PreDestroy();
 
 private:
     // both these functions should be pure virtual
 
 wxDialog::~wxDialog()
 {
     m_isBeingDeleted = TRUE;
+
     delete m_eventLoop;
 
     if (m_mainWidget)
     {
         XtUnmapWidget((Widget) m_mainWidget);
     }
+
+    PreDestroy();
+    DoDestroy();
 }
 
 void wxDialog::DoDestroy()
 
         delete m_frameStatusBar;
         m_frameStatusBar = NULL;
     }
+
+    PreDestroy();
+    DoDestroy();
 }
 
 void wxFrame::DoDestroy()
 {
     Widget frameShell = (Widget)GetShellWidget();
 
-    XtRemoveEventHandler( frameShell, StructureNotifyMask,
-                          False, (XtEventHandler)wxFrameMapProc,
-                          (XtPointer)this );
+    if( frameShell )
+        XtRemoveEventHandler( frameShell, StructureNotifyMask,
+                              False, (XtEventHandler)wxFrameMapProc,
+                              (XtPointer)this );
 
     if( m_clientArea )
     {
 
 // wxTopLevelWindowMotif implementation
 // ===========================================================================
 
-wxTopLevelWindowMotif::~wxTopLevelWindowMotif()
+void wxTopLevelWindowMotif::PreDestroy()
 {
     wxTopLevelWindows.DeleteObject(this);
 
                               wxTLWEventHandler,
                               (XtPointer)this );
     }
+}
 
-    DoDestroy();
-
+wxTopLevelWindowMotif::~wxTopLevelWindowMotif()
+{
     SetMainWidget( (WXWidget)0 );
 
     // If this is the last top-level window, exit.