SDL: Fix memory leak when destroying texture

From 7d5ccae22d2237fd42cbdc09c72620be0ac4892b Mon Sep 17 00:00:00 2001
From: Francisco Javier Trujillo Mata <[EMAIL REDACTED]>
Date: Sun, 14 Aug 2022 12:15:55 +0200
Subject: [PATCH] Fix memory leak when destroying texture

---
 src/render/ps2/SDL_render_ps2.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/render/ps2/SDL_render_ps2.c b/src/render/ps2/SDL_render_ps2.c
index 31f01b67448..b4c7c8f5df7 100644
--- a/src/render/ps2/SDL_render_ps2.c
+++ b/src/render/ps2/SDL_render_ps2.c
@@ -678,9 +678,20 @@ static void
 PS2_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
 {
     GSTEXTURE *ps2_texture = (GSTEXTURE *) texture->driverdata;
+    PS2_RenderData *data = (PS2_RenderData *)renderer->driverdata;
+
+    if (data == 0)
+        return;
 
+    if(ps2_texture == 0)
+        return;
+
+    // Free from vram
+    gsKit_TexManager_free(data->gsGlobal, ps2_texture);
+    
     SDL_free(ps2_texture->Mem);
     SDL_free(ps2_texture);
+    texture->driverdata = NULL;
 }
 
 static void