From 584191773de669a1256d64037ed67d189f21b854 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Fri, 30 Oct 2015 20:17:55 -0400
Subject: [PATCH] stop gap to fix bug with the rendering context disappearing
 on window close, and stray gui_canvas_getscroll calls trying to access
 members of the vanished Window object

---
 pd/nw/pdgui.js | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 7d8f46653..e013ba93d 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -398,6 +398,8 @@ exports.menu_new = menu_new;
 
 function gui_close_window(cid) {
     nw_close_window(patchwin[cid]);
+    // remove reference to the window from patchwin object
+    patchwin[cid] = null;
 }
 
 function menu_k12_open_demos () {
@@ -3088,12 +3090,16 @@ function gui_undo_menu(cid, undo_text, redo_text) {
 
 function do_getscroll(cid) {
     var bbox, width, height, min_width, min_height, x, y,
-        svg = get_item(cid, "patchsvg");
-    // Not sure why I need to check for null here... I'm waiting for the
-    // nw window to load before mapping the Pd canvas, so the patchsvg
-    // should always exist.  Perhaps I also need to set an event for
-    // document.onload as well...
-    if (svg === null) { return; }
+        svg;
+    // Since we're throttling these getscroll calls, they can happen after 
+    // the patch has been closed. We remove the cid from the patchwin 
+    // object on close, so we can just check to see if our Window object has
+    // been set to null, and if so just return.
+    // This is an awfully bad pattern. The whole scroll-checking mechanism
+    // needs to be rethought, but in the meantime this should prevent any
+    // errors wrt the rendering context disappearing.
+    if (!patchwin[cid]) { return; }
+    svg = get_item(cid, "patchsvg");
     bbox = svg.getBBox();
     // We try to do Pd-extended style canvas origins. That is, coord (0, 0) 
     // should be in the top-left corner unless there are objects with a 
-- 
GitLab