Index: /trunk/src/gui/kernel/qwidget_pm.cpp
===================================================================
--- /trunk/src/gui/kernel/qwidget_pm.cpp	(revision 774)
+++ /trunk/src/gui/kernel/qwidget_pm.cpp	(revision 775)
@@ -1389,14 +1389,11 @@
             qApp->d_func()->closePopup(this);
         if (destroyWindow && !(windowType() == Qt::Desktop) &&
-            internalWinId() != NULLHANDLE) {
-            HWND id = internalWinId();
-            if (isWindow() && !(windowType() == Qt::Popup)) {
-                // extra data including extra->topextra has been already
-                // deleted at this point by deleteExtra() and therefore
-                // calling frameWinId() is useless -- it will return the
-                // client window handle. Use WinQueryWindow() instead.
-                id = WinQueryWindow(id, QW_PARENT);
-                Q_ASSERT(id != NULLHANDLE);
-            }
+            d->frameWinId() != NULLHANDLE) {
+            // make sure the backing store is deleted before destroying HWND --
+            // QPMDiveWindowSurface depends on that
+            delete d->topData()->backingStore;
+            d->topData()->backingStore = 0;
+            // now destroy HWND
+            HWND id = d->frameWinId();
 #if defined(QT_DEBUGWINCREATEDESTROY)
             qDebug() << "|Destroying window" << this
@@ -1472,6 +1469,17 @@
     }
 
-    if ((q->windowType() == Qt::Desktop))
-        old_fid = NULLHANDLE;
+    if (old_fid != NULLHANDLE && q->windowType() != Qt::Desktop) {
+        // make sure the backing store is deleted before destroying HWND --
+        // QPMDiveWindowSurface depends on that
+        if (extra && extra->topextra) {
+            delete extra->topextra->backingStore;
+            extra->topextra->backingStore = 0;
+            // zero the to-be-destroyed frame HWND just in case
+            extra->topextra->fId = NULLHANDLE;
+        }
+        // now destroy HWND
+        qt_WinDestroyWindow(old_fid);
+    }
+
     setWinId(0);
 
@@ -1506,6 +1514,4 @@
         setWindowTitle_helper(extra->topextra->caption);
     }
-    if (old_fid != NULLHANDLE)
-        qt_WinDestroyWindow(old_fid);
 
     if (q->testAttribute(Qt::WA_AcceptDrops) || dropSiteWasRegistered
Index: /trunk/src/gui/painting/qwindowsurface_pm.cpp
===================================================================
--- /trunk/src/gui/painting/qwindowsurface_pm.cpp	(revision 774)
+++ /trunk/src/gui/painting/qwindowsurface_pm.cpp	(revision 775)
@@ -368,7 +368,7 @@
         if (widget == that->window())
             return &data;
-        if (!subWidgets || !subWidgets->contains(widget->winId()))
+        if (!subWidgets || !subWidgets->contains(widget->internalWinId()))
             return 0;
-        return &(*subWidgets)[widget->winId()];
+        return &(*subWidgets)[widget->internalWinId()];
     }
 
@@ -379,4 +379,6 @@
 void QPMDiveWindowSurfacePrivate::addWidget(QWidget *widget)
 {
+    Q_ASSERT(widget->internalWinId());
+
     WidgetData *wd = &data;
     if (widget != that->window()) {
@@ -384,5 +386,5 @@
         if (!subWidgets)
             subWidgets = new QMap<HWND, WidgetData>();
-        wd = &(*subWidgets)[widget->winId()];
+        wd = &(*subWidgets)[widget->internalWinId()];
     }
 
@@ -398,10 +400,9 @@
         // to handle other PM messages
         widget->addPmEventFilter(this);
-        WinSetVisibleRegionNotify(widget->winId(), TRUE);
+        WinSetVisibleRegionNotify(widget->internalWinId(), TRUE);
     } else {
         // receive various PM messages
         widget->addPmEventFilter(this);
-        // receive reparent/destruction messages from children
-        // to cleanup the map
+        // receive reparent messages from children to cleanup the map
         widget->installEventFilter(this);
     }
@@ -411,13 +412,11 @@
 {
     if (widget == that->window()) {
-        WinSetVisibleRegionNotify(widget->winId(), FALSE);
+        WinSetVisibleRegionNotify(widget->internalWinId(), FALSE);
         widget->removePmEventFilter(this);
     } else {
         widget->removeEventFilter(this);
         widget->removePmEventFilter(this);
-    }
-
-    if (widget != that->window())
-        subWidgets->remove(widget->winId());
+        subWidgets->remove(widget->internalWinId());
+    }
 }
 
@@ -425,6 +424,5 @@
 {
     QWidget *widget = qobject_cast<QWidget *>(obj);
-    if (event->type() == QEvent::ParentAboutToChange ||
-        event->type() == QEvent::Destroy) {
+    if (event->type() == QEvent::ParentAboutToChange) {
         removeWidget(widget);
     }
@@ -482,5 +480,5 @@
         }
         case WM_SIZE: {
-            if (msg->hwnd != that->window()->winId()) {
+            if (msg->hwnd != that->window()->internalWinId()) {
                 // sometimes PM doesn't send WM_VRNENABLED to the main widget
                 // when the sub-widget (its descendant) gets resized within it;
@@ -611,5 +609,5 @@
         // produce a clip region that excludes all descending obstacles
         // (like child widgets with real HWNDs which are not excluded by Qt)
-        HWND wwnd = widget->winId();
+        HWND wwnd = widget->internalWinId();
         RECTL wrcl = { wbr.left(), wh - wbr.bottom() - 1,
                        wbr.right() + 1, wh - wbr.top() };
@@ -659,5 +657,5 @@
 bool QPMDiveWindowSurface::adjustSetup(QWidget *widget)
 {
-    HWND hwnd = window()->winId();
+    HWND hwnd = window()->internalWinId();
 
     // don't include lScreenPosX and the rest by default
@@ -693,5 +691,5 @@
         ULONG rc = WinQueryVisibleRegion(hwnd, hrgn);
         if (rc != RGN_ERROR) {
-            HWND hwndWidget = widget->winId();
+            HWND hwndWidget = widget->internalWinId();
             POINTL ptlOffset = { 0, 0 };
             if (hwnd != hwndWidget) {
