//we have to get the window procedure while the structurelock\r
       //is still held as the window could be destroyed from another thread\r
       //otherwise.\r
-      windowproc := window.windowproc;\r
+      if window <> nil then begin
+        windowproc := window.windowproc;\r
+      end else begin
+        windowproc := nil;
+      end;
     finally\r
       structurelock.release;\r
     end;\r
-    if window <> nil then begin\r
+    if windowproc <> nil then begin\r
       result := windowproc(lpmsg.hwnd,lpmsg.message,lpmsg.wparam,lpmsg.lparam);\r
     end else begin\r
       result := -1;\r