patch 8.0.0331: restoring help snapshot accesses freed memory

Problem:    Restoring help snapshot accesses freed memory. (Dominique Pelle)
Solution:   Don't restore a snapshot when the window closes.
diff --git a/src/Makefile b/src/Makefile
index e1443e5..153e526 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -2132,6 +2132,7 @@
 	test_goto \
 	test_gui \
 	test_hardcopy \
+	test_help \
 	test_help_tagjump \
 	test_hide \
 	test_history \
diff --git a/src/testdir/Make_all.mak b/src/testdir/Make_all.mak
index 58291ea..3ff7247 100644
--- a/src/testdir/Make_all.mak
+++ b/src/testdir/Make_all.mak
@@ -154,6 +154,7 @@
 	    test_gn.res \
 	    test_gui.res \
 	    test_hardcopy.res \
+	    test_help.res \
 	    test_hide.res \
 	    test_history.res \
 	    test_hlsearch.res \
diff --git a/src/testdir/test_help.vim b/src/testdir/test_help.vim
new file mode 100644
index 0000000..ca095d0
--- /dev/null
+++ b/src/testdir/test_help.vim
@@ -0,0 +1,10 @@
+" Tests for :help
+
+func Test_help_restore_snapshot()
+  help
+  set buftype=
+  help
+  edit x
+  help
+  helpclose
+endfunc
diff --git a/src/version.c b/src/version.c
index 906c30c..b014e74 100644
--- a/src/version.c
+++ b/src/version.c
@@ -765,6 +765,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    331,
+/**/
     330,
 /**/
     329,
diff --git a/src/window.c b/src/window.c
index 6b7bd35..c9771af 100644
--- a/src/window.c
+++ b/src/window.c
@@ -6551,7 +6551,7 @@
 
 /*
  * Check if frames "sn" and "fr" have the same layout, same following frames
- * and same children.
+ * and same children.  And the window pointer is valid.
  */
     static int
 check_snapshot_rec(frame_T *sn, frame_T *fr)
@@ -6562,7 +6562,8 @@
 	    || (sn->fr_next != NULL
 		&& check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
 	    || (sn->fr_child != NULL
-		&& check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
+		&& check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
+	    || !win_valid(sn->fr_win))
 	return FAIL;
     return OK;
 }