tests: plug leaks when running with --trackmem

(using SDL_VIDEO_DRIVER=x11/wayland on Linux)
This commit is contained in:
Anonymous Maarten 2023-08-22 18:44:28 +02:00 committed by Anonymous Maarten
parent f42bbeca24
commit 2a01f9dcb5
11 changed files with 93 additions and 97 deletions

View file

@ -15,8 +15,6 @@
pump the event loop and catch keystrokes. pump the event loop and catch keystrokes.
*/ */
#include <stdlib.h>
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#endif #endif
@ -25,22 +23,10 @@
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h> #include <SDL3/SDL_test.h>
static SDL_Window *window; static SDLTest_CommonState *state;
static SDL_Renderer *renderer;
static SDLTest_TextWindow *textwin; static SDLTest_TextWindow *textwin;
static int done; 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 static void
print_string(char **text, size_t *maxlen, const char *fmt, ...) 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) static void loop(void)
{ {
SDL_Event event; SDL_Event event;
int i;
/* Check for events */ /* Check for events */
/*SDL_WaitEvent(&event); emscripten does not like waiting*/ /*SDL_WaitEvent(&event); emscripten does not like waiting*/
@ -234,11 +221,13 @@ static void loop(void)
} }
} }
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); for (i = 0; i < state->num_windows; i++) {
SDL_RenderClear(renderer); SDL_SetRenderDrawColor(state->renderers[i], 0, 0, 0, 255);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderClear(state->renderers[i]);
SDLTest_TextWindowDisplay(textwin, renderer); SDL_SetRenderDrawColor(state->renderers[i], 255, 255, 255, 255);
SDL_RenderPresent(renderer); SDLTest_TextWindowDisplay(textwin, state->renderers[i]);
SDL_RenderPresent(state->renderers[i]);
}
/* Slow down framerate */ /* Slow down framerate */
SDL_Delay(100); SDL_Delay(100);
@ -252,13 +241,14 @@ static void loop(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
SDLTest_CommonState *state; int w, h;
/* Initialize test framework */ /* Initialize test framework */
state = SDLTest_CommonCreateState(argv, 0); state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (state == NULL) { if (state == NULL) {
return 1; return 1;
} }
state->window_title = "CheckKeys Test";
/* Enable standard application logging */ /* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); 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"); SDL_SetHint(SDL_HINT_IME_SUPPORT_EXTENDED_TEXT, "1");
/* Initialize SDL */ /* 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()); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
return 1; return 1;
} }
/* Set 640x480 video mode */ SDL_GetWindowSize(state->windows[0], &w, &h);
window = SDL_CreateWindow("CheckKeys Test", 640, 480, 0); textwin = SDLTest_TextWindowCreate(0.f, 0.f, (float)w, (float)h);
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);
#ifdef __IOS__ #ifdef __IOS__
{
int i;
/* Creating the context creates the view, which we need to show keyboard */ /* Creating the context creates the view, which we need to show keyboard */
SDL_GL_CreateContext(window); for (i = 0; i < state->num_windows; i++) {
SDL_GL_CreateContext(state->windows[i]);
}
}
#endif #endif
SDL_StartTextInput(); SDL_StartTextInput();
@ -319,7 +300,8 @@ int main(int argc, char *argv[])
} }
#endif #endif
SDL_Quit(); SDLTest_TextWindowDestroy(textwin);
SDLTest_CommonDestroyState(state); SDLTest_CleanupTextDrawing();
SDLTest_CommonQuit(state);
return 0; return 0;
} }

View file

@ -303,6 +303,9 @@ int main(int argc, char *argv[])
} }
#endif #endif
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_WaitThread(thread, NULL); SDL_WaitThread(thread, NULL);
SDL_Quit(); SDL_Quit();
SDLTest_CommonDestroyState(state); SDLTest_CommonDestroyState(state);

View file

@ -440,6 +440,7 @@ int main(int argc, char *argv[])
} }
#endif #endif
SDLTest_CleanupTextDrawing();
SDL_DestroyAudioStream(stream); SDL_DestroyAudioStream(stream);
SDL_free(audio_buf); SDL_free(audio_buf);
SDLTest_CommonQuit(state); SDLTest_CommonQuit(state);

View file

@ -2008,15 +2008,22 @@ int main(int argc, char *argv[])
HandleGamepadRemoved(controllers[0].id); HandleGamepadRemoved(controllers[0].id);
DelController(controllers[0].id); DelController(controllers[0].id);
} }
SDL_free(controllers);
SDL_free(controller_name);
DestroyGamepadImage(image); DestroyGamepadImage(image);
DestroyGamepadDisplay(gamepad_elements); DestroyGamepadDisplay(gamepad_elements);
DestroyGamepadTypeDisplay(gamepad_type); DestroyGamepadTypeDisplay(gamepad_type);
DestroyJoystickDisplay(joystick_elements); DestroyJoystickDisplay(joystick_elements);
DestroyGamepadButton(setup_mapping_button);
DestroyGamepadButton(done_mapping_button);
DestroyGamepadButton(cancel_button);
DestroyGamepadButton(clear_button);
DestroyGamepadButton(copy_button); DestroyGamepadButton(copy_button);
DestroyGamepadButton(paste_button);
SDLTest_CleanupTextDrawing();
SDL_DestroyRenderer(screen); SDL_DestroyRenderer(screen);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD); SDL_Quit();
SDLTest_CommonDestroyState(state); SDLTest_CommonDestroyState(state);
return 0; return 0;
} }

View file

@ -10,24 +10,11 @@
freely. freely.
*/ */
#include <stdlib.h>
#include <SDL3/SDL_test_common.h> #include <SDL3/SDL_test_common.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
static SDLTest_CommonState *state; 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 main(int argc, char *argv[])
{ {
int i, done; int i, done;
@ -35,6 +22,7 @@ int main(int argc, char *argv[])
SDL_bool is_hover = SDL_FALSE; SDL_bool is_hover = SDL_FALSE;
float x = 0.0f, y = 0.0f; float x = 0.0f, y = 0.0f;
unsigned int windowID = 0; unsigned int windowID = 0;
int return_code = -1;
/* Initialize test framework */ /* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
@ -58,12 +46,14 @@ int main(int argc, char *argv[])
} }
if (consumed < 0) { if (consumed < 0) {
SDLTest_CommonLogUsage(state, argv[0], NULL); SDLTest_CommonLogUsage(state, argv[0], NULL);
quit(1); return_code = 1;
goto quit;
} }
i += consumed; i += consumed;
} }
if (!SDLTest_CommonInit(state)) { if (!SDLTest_CommonInit(state)) {
quit(2); return_code = 1;
goto quit;
} }
@ -114,7 +104,8 @@ int main(int argc, char *argv[])
SDL_Delay(16); SDL_Delay(16);
} }
quit(0); return_code = 0;
/* keep the compiler happy ... */ quit:
return 0; SDLTest_CommonQuit(state);
return return_code;
} }

View file

@ -369,6 +369,7 @@ int main(int argc, char *argv[])
SDL_RWclose(rwops); SDL_RWclose(rwops);
SDL_Log("test5 OK\n"); SDL_Log("test5 OK\n");
cleanup(); cleanup();
SDL_Quit();
SDLTest_CommonDestroyState(state); SDLTest_CommonDestroyState(state);
return 0; /* all ok */ return 0; /* all ok */
} }

View file

@ -212,6 +212,12 @@ int main(int argc, char *argv[])
MoveSprites(renderer, sprite); MoveSprites(renderer, sprite);
} }
SDL_DestroyTexture(sprite);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_free(positions);
SDL_free(velocities);
quit(0); quit(0);
return 0; /* to prevent compiler warning */ return 0; /* to prevent compiler warning */

View file

@ -388,6 +388,15 @@ ret:
/* Destroy the window. */ /* Destroy the window. */
SDL_DestroyWindow(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(); SDL_Quit();
SDLTest_CommonDestroyState(state); SDLTest_CommonDestroyState(state);

View file

@ -35,17 +35,6 @@ static int sprite_w, sprite_h;
static SDL_Renderer *renderer; static SDL_Renderer *renderer;
static int done; 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) static void MoveSprites(void)
{ {
int i; int i;
@ -100,7 +89,8 @@ static void loop(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
SDL_Window *window; SDL_Window *window = NULL;
int return_code = -1;
int i; int i;
/* Enable standard application logging */ /* Enable standard application logging */
@ -108,17 +98,24 @@ int main(int argc, char *argv[])
if (argc > 1) { if (argc > 1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "USAGE: %s\n", argv[0]); 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) { 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); sprite = LoadTexture(renderer, "icon.bmp", SDL_TRUE, &sprite_w, &sprite_h);
if (sprite == NULL) { if (sprite == NULL) {
quit(2); return_code = 3;
goto quit;
} }
/* Initialize the sprite positions */ /* Initialize the sprite positions */
@ -146,7 +143,10 @@ int main(int argc, char *argv[])
loop(); loop();
} }
#endif #endif
quit(0); return_code = 0;
quit:
return 0; /* to prevent compiler warning */ SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return return_code;
} }

View file

@ -10,8 +10,6 @@
freely. freely.
*/ */
#include <stdlib.h>
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#endif #endif
@ -42,17 +40,6 @@ static SDL_Cursor *cursor = NULL;
static SDL_bool relative_mode = SDL_FALSE; static SDL_bool relative_mode = SDL_FALSE;
static const SDL_DisplayMode *highlighted_mode = NULL; 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 */ /* Draws the modes menu, and stores the mode index under the mouse in highlighted_mode */
static void static void
draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport) draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
@ -298,7 +285,7 @@ int main(int argc, char *argv[])
#endif #endif
SDL_DestroyCursor(cursor); SDL_DestroyCursor(cursor);
quit(0); SDLTest_CleanupTextDrawing();
/* keep the compiler happy ... */ SDLTest_CommonQuit(state);
return 0; return 0;
} }

View file

@ -243,6 +243,7 @@ int main(int argc, char **argv)
char *filename = NULL; char *filename = NULL;
SDL_Surface *original; SDL_Surface *original;
SDL_Surface *converted; SDL_Surface *converted;
SDL_Surface *bmp;
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
SDL_Texture *output[3]; SDL_Texture *output[3];
@ -368,7 +369,9 @@ int main(int argc, char **argv)
} }
filename = GetResourceFilename(filename, "testyuv.bmp"); 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) { if (original == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
return 3; return 3;
@ -481,7 +484,13 @@ int main(int argc, char **argv)
SDL_Delay(10); SDL_Delay(10);
} }
} }
SDL_free(raw_yuv);
SDL_free(filename); SDL_free(filename);
SDL_DestroySurface(original);
SDL_DestroySurface(converted);
SDLTest_CleanupTextDrawing();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
SDLTest_CommonDestroyState(state); SDLTest_CommonDestroyState(state);
return 0; return 0;