From 2a01f9dcb5afdb7059b88e5c9a35e3cee0f46b3b Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Tue, 22 Aug 2023 18:44:28 +0200
Subject: [PATCH] tests: plug leaks when running with --trackmem
(using SDL_VIDEO_DRIVER=x11/wayland on Linux)
---
test/checkkeys.c | 68 ++++++++++-----------------
test/checkkeysthreads.c | 3 ++
test/testaudiostreamdynamicresample.c | 1 +
test/testcontroller.c | 11 ++++-
test/testdropfile.c | 27 ++++-------
test/testfile.c | 1 +
test/testnative.c | 6 +++
test/testshape.c | 9 ++++
test/testspriteminimal.c | 36 +++++++-------
test/testwm.c | 17 +------
test/testyuv.c | 11 ++++-
11 files changed, 93 insertions(+), 97 deletions(-)
diff --git a/test/checkkeys.c b/test/checkkeys.c
index 89bae8eeee32..49f39231523c 100644
--- a/test/checkkeys.c
+++ b/test/checkkeys.c
@@ -15,8 +15,6 @@
pump the event loop and catch keystrokes.
*/
-#include <stdlib.h>
-
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
@@ -25,22 +23,10 @@
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
-static SDL_Window *window;
-static SDL_Renderer *renderer;
+static SDLTest_CommonState *state;
static SDLTest_TextWindow *textwin;
static int done;
-/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
-static void
-quit(int rc)
-{
- SDL_Quit();
- /* Let 'main()' return normally */
- if (rc != 0) {
- exit(rc);
- }
-}
-
static void
print_string(char **text, size_t *maxlen, const char *fmt, ...)
{
@@ -171,6 +157,7 @@ PrintText(const char *eventtype, const char *text)
static void loop(void)
{
SDL_Event event;
+ int i;
/* Check for events */
/*SDL_WaitEvent(&event); emscripten does not like waiting*/
@@ -234,11 +221,13 @@ static void loop(void)
}
}
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- SDL_RenderClear(renderer);
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
- SDLTest_TextWindowDisplay(textwin, renderer);
- SDL_RenderPresent(renderer);
+ for (i = 0; i < state->num_windows; i++) {
+ SDL_SetRenderDrawColor(state->renderers[i], 0, 0, 0, 255);
+ SDL_RenderClear(state->renderers[i]);
+ SDL_SetRenderDrawColor(state->renderers[i], 255, 255, 255, 255);
+ SDLTest_TextWindowDisplay(textwin, state->renderers[i]);
+ SDL_RenderPresent(state->renderers[i]);
+ }
/* Slow down framerate */
SDL_Delay(100);
@@ -252,13 +241,14 @@ static void loop(void)
int main(int argc, char *argv[])
{
- SDLTest_CommonState *state;
+ int w, h;
/* Initialize test framework */
- state = SDLTest_CommonCreateState(argv, 0);
+ state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (state == NULL) {
return 1;
}
+ state->window_title = "CheckKeys Test";
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
@@ -275,31 +265,22 @@ int main(int argc, char *argv[])
SDL_SetHint(SDL_HINT_IME_SUPPORT_EXTENDED_TEXT, "1");
/* Initialize SDL */
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+ if (!SDLTest_CommonInit(state)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
return 1;
}
- /* Set 640x480 video mode */
- window = SDL_CreateWindow("CheckKeys Test", 640, 480, 0);
- if (window == NULL) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
- SDL_GetError());
- quit(2);
- }
-
- renderer = SDL_CreateRenderer(window, NULL, 0);
- if (renderer == NULL) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n",
- SDL_GetError());
- quit(2);
- }
-
- textwin = SDLTest_TextWindowCreate(0, 0, 640, 480);
+ SDL_GetWindowSize(state->windows[0], &w, &h);
+ textwin = SDLTest_TextWindowCreate(0.f, 0.f, (float)w, (float)h);
#ifdef __IOS__
- /* Creating the context creates the view, which we need to show keyboard */
- SDL_GL_CreateContext(window);
+ {
+ int i;
+ /* Creating the context creates the view, which we need to show keyboard */
+ for (i = 0; i < state->num_windows; i++) {
+ SDL_GL_CreateContext(state->windows[i]);
+ }
+ }
#endif
SDL_StartTextInput();
@@ -319,7 +300,8 @@ int main(int argc, char *argv[])
}
#endif
- SDL_Quit();
- SDLTest_CommonDestroyState(state);
+ SDLTest_TextWindowDestroy(textwin);
+ SDLTest_CleanupTextDrawing();
+ SDLTest_CommonQuit(state);
return 0;
}
diff --git a/test/checkkeysthreads.c b/test/checkkeysthreads.c
index 89d5188d6f29..811584c09b5a 100644
--- a/test/checkkeysthreads.c
+++ b/test/checkkeysthreads.c
@@ -303,6 +303,9 @@ int main(int argc, char *argv[])
}
#endif
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+
SDL_WaitThread(thread, NULL);
SDL_Quit();
SDLTest_CommonDestroyState(state);
diff --git a/test/testaudiostreamdynamicresample.c b/test/testaudiostreamdynamicresample.c
index 8acd02e0b5ce..9dacc87d5a8f 100644
--- a/test/testaudiostreamdynamicresample.c
+++ b/test/testaudiostreamdynamicresample.c
@@ -440,6 +440,7 @@ int main(int argc, char *argv[])
}
#endif
+ SDLTest_CleanupTextDrawing();
SDL_DestroyAudioStream(stream);
SDL_free(audio_buf);
SDLTest_CommonQuit(state);
diff --git a/test/testcontroller.c b/test/testcontroller.c
index fa9e7e7dca93..dd4b9819d95f 100644
--- a/test/testcontroller.c
+++ b/test/testcontroller.c
@@ -2008,15 +2008,22 @@ int main(int argc, char *argv[])
HandleGamepadRemoved(controllers[0].id);
DelController(controllers[0].id);
}
+ SDL_free(controllers);
+ SDL_free(controller_name);
DestroyGamepadImage(image);
DestroyGamepadDisplay(gamepad_elements);
DestroyGamepadTypeDisplay(gamepad_type);
DestroyJoystickDisplay(joystick_elements);
+ DestroyGamepadButton(setup_mapping_button);
+ DestroyGamepadButton(done_mapping_button);
+ DestroyGamepadButton(cancel_button);
+ DestroyGamepadButton(clear_button);
DestroyGamepadButton(copy_button);
+ DestroyGamepadButton(paste_button);
+ SDLTest_CleanupTextDrawing();
SDL_DestroyRenderer(screen);
SDL_DestroyWindow(window);
- SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD);
+ SDL_Quit();
SDLTest_CommonDestroyState(state);
-
return 0;
}
diff --git a/test/testdropfile.c b/test/testdropfile.c
index acb016122f6e..2e4efedbab98 100644
--- a/test/testdropfile.c
+++ b/test/testdropfile.c
@@ -10,24 +10,11 @@
freely.
*/
-#include <stdlib.h>
-
#include <SDL3/SDL_test_common.h>
#include <SDL3/SDL_main.h>
static SDLTest_CommonState *state;
-/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
-static void
-quit(int rc)
-{
- SDLTest_CommonQuit(state);
- /* Let 'main()' return normally */
- if (rc != 0) {
- exit(rc);
- }
-}
-
int main(int argc, char *argv[])
{
int i, done;
@@ -35,6 +22,7 @@ int main(int argc, char *argv[])
SDL_bool is_hover = SDL_FALSE;
float x = 0.0f, y = 0.0f;
unsigned int windowID = 0;
+ int return_code = -1;
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
@@ -58,12 +46,14 @@ int main(int argc, char *argv[])
}
if (consumed < 0) {
SDLTest_CommonLogUsage(state, argv[0], NULL);
- quit(1);
+ return_code = 1;
+ goto quit;
}
i += consumed;
}
if (!SDLTest_CommonInit(state)) {
- quit(2);
+ return_code = 1;
+ goto quit;
}
@@ -114,7 +104,8 @@ int main(int argc, char *argv[])
SDL_Delay(16);
}
- quit(0);
- /* keep the compiler happy ... */
- return 0;
+ return_code = 0;
+quit:
+ SDLTest_CommonQuit(state);
+ return return_code;
}
diff --git a/test/testfile.c b/test/testfile.c
index 8aa48007fef6..1561f176d370 100644
--- a/test/testfile.c
+++ b/test/testfile.c
@@ -369,6 +369,7 @@ int main(int argc, char *argv[])
SDL_RWclose(rwops);
SDL_Log("test5 OK\n");
cleanup();
+ SDL_Quit();
SDLTest_CommonDestroyState(state);
return 0; /* all ok */
}
diff --git a/test/testnative.c b/test/testnative.c
index ef587ede7729..166e5d8c9a98 100644
--- a/test/testnative.c
+++ b/test/testnative.c
@@ -212,6 +212,12 @@ int main(int argc, char *argv[])
MoveSprites(renderer, sprite);
}
+ SDL_DestroyTexture(sprite);
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_free(positions);
+ SDL_free(velocities);
+
quit(0);
return 0; /* to prevent compiler warning */
diff --git a/test/testshape.c b/test/testshape.c
index ae5ee560bdcf..078319794a99 100644
--- a/test/testshape.c
+++ b/test/testshape.c
@@ -388,6 +388,15 @@ int main(int argc, char **argv)
/* Destroy the window. */
SDL_DestroyWindow(window);
+ if (g_bitmap) {
+ SDL_free(g_bitmap);
+ g_bitmap = NULL;
+ }
+ if (g_shape_surface) {
+ SDL_DestroySurface(g_shape_surface);
+ g_shape_surface = NULL;
+ }
+
SDL_Quit();
SDLTest_CommonDestroyState(state);
diff --git a/test/testspriteminimal.c b/test/testspriteminimal.c
index b58d21d6eff6..e78da6fe2ddc 100644
--- a/test/testspriteminimal.c
+++ b/test/testspriteminimal.c
@@ -35,17 +35,6 @@ static int sprite_w, sprite_h;
static SDL_Renderer *renderer;
static int done;
-/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
-static void
-quit(int rc)
-{
- SDL_Quit();
- /* Let 'main()' return normally */
- if (rc != 0) {
- exit(rc);
- }
-}
-
static void MoveSprites(void)
{
int i;
@@ -100,7 +89,8 @@ static void loop(void)
int main(int argc, char *argv[])
{
- SDL_Window *window;
+ SDL_Window *window = NULL;
+ int return_code = -1;
int i;
/* Enable standard application logging */
@@ -108,17 +98,24 @@ int main(int argc, char *argv[])
if (argc > 1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "USAGE: %s\n", argv[0]);
- quit(1);
+ return_code = 1;
+ goto quit;
}
if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
- quit(2);
+ return_code = 2;
+ goto quit;
+ }
+
+ if (SDL_SetWindowTitle(window, argv[0]) < 0) {
+ SDL_Log("SDL_SetWindowTitle: %s", SDL_GetError());
}
sprite = LoadTexture(renderer, "icon.bmp", SDL_TRUE, &sprite_w, &sprite_h);
if (sprite == NULL) {
- quit(2);
+ return_code = 3;
+ goto quit;
}
/* Initialize the sprite positions */
@@ -146,7 +143,10 @@ int main(int argc, char *argv[])
loop();
}
#endif
- quit(0);
-
- return 0; /* to prevent compiler warning */
+ return_code = 0;
+quit:
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+ return return_code;
}
diff --git a/test/testwm.c b/test/testwm.c
index 4e4057ec3e5d..9116700a2ebe 100644
--- a/test/testwm.c
+++ b/test/testwm.c
@@ -10,8 +10,6 @@
freely.
*/
-#include <stdlib.h>
-
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
@@ -42,17 +40,6 @@ static SDL_Cursor *cursor = NULL;
static SDL_bool relative_mode = SDL_FALSE;
static const SDL_DisplayMode *highlighted_mode = NULL;
-/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
-static void
-quit(int rc)
-{
- SDLTest_CommonQuit(state);
- /* Let 'main()' return normally */
- if (rc != 0) {
- exit(rc);
- }
-}
-
/* Draws the modes menu, and stores the mode index under the mouse in highlighted_mode */
static void
draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
@@ -298,7 +285,7 @@ int main(int argc, char *argv[])
#endif
SDL_DestroyCursor(cursor);
- quit(0);
- /* keep the compiler happy ... */
+ SDLTest_CleanupTextDrawing();
+ SDLTest_CommonQuit(state);
return 0;
}
diff --git a/test/testyuv.c b/test/testyuv.c
index 7e587b84709e..afe3cac518aa 100644
--- a/test/testyuv.c
+++ b/test/testyuv.c
@@ -243,6 +243,7 @@ int main(int argc, char **argv)
char *filename = NULL;
SDL_Surface *original;
SDL_Surface *converted;
+ SDL_Surface *bmp;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *output[3];
@@ -368,7 +369,9 @@ int main(int argc, char **argv)
}
filename = GetResourceFilename(filename, "testyuv.bmp");
- original = SDL_ConvertSurfaceFormat(SDL_LoadBMP(filename), SDL_PIXELFORMAT_RGB24);
+ bmp = SDL_LoadBMP(filename);
+ original = SDL_ConvertSurfaceFormat(bmp, SDL_PIXELFORMAT_RGB24);
+ SDL_DestroySurface(bmp);
if (original == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
return 3;
@@ -481,7 +484,13 @@ int main(int argc, char **argv)
SDL_Delay(10);
}
}
+ SDL_free(raw_yuv);
SDL_free(filename);
+ SDL_DestroySurface(original);
+ SDL_DestroySurface(converted);
+ SDLTest_CleanupTextDrawing();
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
SDL_Quit();
SDLTest_CommonDestroyState(state);
return 0;