diff --git a/gdk-pixbuf/gdk-pixbuf.c b/gdk-pixbuf/gdk-pixbuf.c
index 27e5730cb..c08bddbe2 100644
--- a/gdk-pixbuf/gdk-pixbuf.c
+++ b/gdk-pixbuf/gdk-pixbuf.c
@@ -715,6 +715,8 @@ gdk_pixbuf_get_pixels_with_length (const GdkPixbuf *pixbuf,
                 gsize len;
                 mut_pixbuf->pixels = g_bytes_unref_to_data (pixbuf->bytes, &len);
                 mut_pixbuf->bytes = NULL;
+                mut_pixbuf->destroy_fn = free_buffer;
+                mut_pixbuf->destroy_fn_data = NULL;
         }
 
         if (length)
