diff --git a/ChangeLog b/ChangeLog
index db8ae682be894b7783efd79a0ec906a5faaac916..f4c8a3a8607cc5247cdec72fcebf98380db566d4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,9 @@
 	* src/files.c (set_modified): Move this function to its habitat.
 	* src/files.c (open_file): Return the fantastic file descriptor
 	when opening a non-existent file for reading succeeds.
+	* src/nano.c (delete_opennode), src/text.c (discard_until):
+	Free the items on the undo stack when a buffer is closed.
+	This fixes Savannah bug #46904 reported by Mike Frysinger.
 
 2016-01-15  Mike Frysinger  <vapier@gentoo.org>
 	* src/files.c (open_file): Free the full filename in all cases.
diff --git a/src/nano.c b/src/nano.c
index 05d98ce75da215a2231008e4f65fd8bd66cd48ec..46c78b6bcbcf8d3a6f98a9f49de6266e87d2ac5d 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -561,6 +561,8 @@ void delete_opennode(openfilestruct *fileptr)
 #ifndef NANO_TINY
     free(fileptr->current_stat);
     free(fileptr->lock_filename);
+    /* Free the undo stack. */
+    discard_until(NULL, fileptr);
 #endif
     free(fileptr);
 }
diff --git a/src/proto.h b/src/proto.h
index d48f04f5551495d901a70026d06d21d8bd33a265..e0842b4882c42609d2ade8f577be6ae67b4bb2e3 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -748,7 +748,7 @@ void new_magicline(void);
 void remove_magicline(void);
 void mark_order(const filestruct **top, size_t *top_x, const filestruct
 	**bot, size_t *bot_x, bool *right_side_up);
-void discard_until(undo *thisone);
+void discard_until(const undo *thisitem, openfilestruct *thefile);
 void add_undo(undo_type action);
 void update_undo(undo_type action);
 #endif
diff --git a/src/text.c b/src/text.c
index d30b8d87b48b823b6055304f8f79a43eab969c95..da475b2587fd802cc28bc78a3fc736964093270a 100644
--- a/src/text.c
+++ b/src/text.c
@@ -391,7 +391,7 @@ void do_indent(ssize_t cols)
     if (indent_changed) {
 	/* Throw away the undo stack, to prevent making mistakes when
 	 * the user tries to undo something in the reindented text. */
-	discard_until(NULL);
+	discard_until(NULL, openfile);
 	openfile->current_undo = NULL;
 
 	/* Mark the file as modified. */
@@ -893,18 +893,18 @@ bool execute_command(const char *command)
     return TRUE;
 }
 
-/* Discard undo items that are newer than thisone, or all if NULL. */
-void discard_until(undo *thisone)
+/* Discard undo items that are newer than the given one, or all if NULL. */
+void discard_until(const undo *thisitem, openfilestruct *thefile)
 {
-    undo *dropit = openfile->undotop;
+    undo *dropit = thefile->undotop;
 
-    while (dropit != NULL && dropit != thisone) {
-	openfile->undotop = dropit->next;
+    while (dropit != NULL && dropit != thisitem) {
+	thefile->undotop = dropit->next;
 	free(dropit->strdata);
 	if (dropit->cutbuffer != NULL)
 	    free_filestruct(dropit->cutbuffer);
 	free(dropit);
-	dropit = openfile->undotop;
+	dropit = thefile->undotop;
     }
 }
 
@@ -922,7 +922,7 @@ void add_undo(undo_type action)
 	return;
 
     /* Blow away newer undo items if we add somewhere in the middle. */
-    discard_until(u);
+    discard_until(u, openfile);
 
 #ifdef DEBUG
     fprintf(stderr, "  >> Adding an undo...\n");
@@ -2298,7 +2298,7 @@ void do_justify(bool full_justify)
 #ifndef NANO_TINY
 	/* Throw away the entire undo stack, to prevent a crash when
 	 * the user tries to undo something in the justified text. */
-	discard_until(NULL);
+	discard_until(NULL, openfile);
 	openfile->current_undo = NULL;
 #endif
 	/* Blow away the text in the justify buffer. */