SDL: SDL_IOFromMem/SDL_IOFromConstMem: free userdata on close

From 572e088b8f5ac3c72a58fcb02093e6467dd17ecc Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 15 Mar 2024 23:55:29 +0100
Subject: [PATCH] SDL_IOFromMem/SDL_IOFromConstMem: free userdata on close

---
 src/file/SDL_iostream.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/file/SDL_iostream.c b/src/file/SDL_iostream.c
index 6562d0a5fa1f4..050157efab5bb 100644
--- a/src/file/SDL_iostream.c
+++ b/src/file/SDL_iostream.c
@@ -505,6 +505,11 @@ static size_t SDLCALL mem_write(void *userdata, const void *ptr, size_t size, SD
     return mem_io(userdata, iodata->here, ptr, size);
 }
 
+static int SDLCALL mem_close(void *userdata) {
+    SDL_free(userdata);
+    return 0;
+}
+
 /* Functions to create SDL_IOStream structures from various data sources */
 
 #if defined(HAVE_STDIO_H) && !defined(SDL_PLATFORM_WINDOWS)
@@ -679,6 +684,7 @@ SDL_IOStream *SDL_IOFromMem(void *mem, size_t size)
     iface.seek = mem_seek;
     iface.read = mem_read;
     iface.write = mem_write;
+    iface.close = mem_close;
 
     iodata->base = (Uint8 *)mem;
     iodata->here = iodata->base;
@@ -712,6 +718,7 @@ SDL_IOStream *SDL_IOFromConstMem(const void *mem, size_t size)
     iface.seek = mem_seek;
     iface.read = mem_read;
     // leave iface.write as NULL.
+    iface.close = mem_close;
 
     iodata->base = (Uint8 *)mem;
     iodata->here = iodata->base;