From 1ea1fd7899f56258619deeac82b6533200cec263 Mon Sep 17 00:00:00 2001 From: xesf Date: Sun, 20 Oct 2024 21:37:37 +0100 Subject: [PATCH] fix memory leaks --- src/game.c | 3 ++- src/sample.c | 5 ++--- src/system_sdl.c | 14 +++++--------- src/system_sdl_mixer.c | 11 ++++++++++- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/game.c b/src/game.c index 47917d2..ade6755 100644 --- a/src/game.c +++ b/src/game.c @@ -89,8 +89,9 @@ void game_introduction() { i32 sample_hidden_index = 0; u8 *sample_ptr = NULL; u32 entry_size = hqr_get_hidden_entry_ptr(&sample_ptr, menu_samples, 0); - sample_play_ptr(HQR_MENU_SAMPLES * 100 + sample_hidden_index, sample_ptr, entry_size, 22050, 0, 0); + memory_free(sample_ptr); + memory_free(menu_samples); } void game_release(state_t *state) { diff --git a/src/sample.c b/src/sample.c index 3ccbba2..3d545d6 100644 --- a/src/sample.c +++ b/src/sample.c @@ -43,9 +43,10 @@ void sample_set_position(i32 channel_index, i32 x, i32 y, i32 z, i32 hero_x, i32 void sample_play(i32 index, i32 frequency, i32 repeat, i32 pan) { i32 sample_size = 0; - u8* sample_ptr; + u8* sample_ptr = NULL; sample_size = hqr_get_entry_alloc(&sample_ptr, HQR_RESOURCE, index); sample_play_position(index, sample_ptr, sample_size, frequency, repeat, 0, 0, 0, -1, 0, 0, 0); + memory_free(sample_ptr); } void sample_play_ptr(i32 index, u8* sample_ptr, i32 sample_size, i32 frequency, i32 repeat, i32 pan) { @@ -71,8 +72,6 @@ void sample_play_position(i32 index, u8* sample_ptr, i32 sample_size, i32 freque if (system_mixer_play(sample_ptr, sample_size, channel_index, repeat) == -1) printf("Error while playing Sample %d \n", index); } - - free(sample_ptr); } void sample_resume() { diff --git a/src/system_sdl.c b/src/system_sdl.c index 62344ad..8cb9de8 100644 --- a/src/system_sdl.c +++ b/src/system_sdl.c @@ -27,19 +27,13 @@ void system_init(system_t *system, c8 *title, i32 width, i32 height, i32 bpp) { SDL_SetRenderLogicalPresentation(system->renderer, width, height, SDL_LOGICAL_PRESENTATION_LETTERBOX); SDL_SetRenderDrawColor(system->renderer, 0, 0, 0, 255); - system->texture = SDL_CreateTexture( - system->renderer, - SDL_PIXELFORMAT_RGBA8888, // SDL_PIXELFORMAT_INDEX8 - SDL_TEXTUREACCESS_STATIC, - width, - height - ); - SDL_UpdateWindowSurface(system->window); } void system_release(system_t *system) { SDL_DestroyTexture(system->texture); + SDL_DestroySurface(system->surface); + SDL_DestroyPalette(system->palette); SDL_DestroyRenderer(system->renderer); SDL_DestroyWindow(system->window); SDL_Quit(); @@ -102,6 +96,9 @@ void system_create_surface(system_t *system, u8 *front_buffer) { void system_blit(system_t *system) { SDL_RenderClear(system->renderer); + if (system->texture) { + SDL_DestroyTexture(system->texture); + } system->texture = SDL_CreateTextureFromSurface(system->renderer, system->surface); SDL_RenderTexture(system->renderer, system->texture, NULL, NULL); } @@ -111,7 +108,6 @@ inline void system_flip(system_t *system) { } void system_set_palette(system_t *system, u8 *palette) { - // SDL_SetPaletteColors(system->palette, (SDL_Color *)palette, 0, 256); for (i32 i = 0; i < 256; i++) { system->palette->colors[i].r = palette[i * 3 + 0]; system->palette->colors[i].g = palette[i * 3 + 1]; diff --git a/src/system_sdl_mixer.c b/src/system_sdl_mixer.c index 4d2da3a..0000db2 100644 --- a/src/system_sdl_mixer.c +++ b/src/system_sdl_mixer.c @@ -54,7 +54,11 @@ inline void system_mixer_set_distance(i32 channel_index, i32 distance) { inline void system_mixer_load(u8 *sample_ptr, i32 samples_size) { SDL_IOStream *rw = SDL_IOFromMem(sample_ptr, samples_size); + if (chunk != NULL) { + Mix_FreeChunk(chunk); + } chunk = Mix_LoadWAV_IO(rw, 1); + SDL_FlushIO(rw); } inline void system_mixer_free() { @@ -98,7 +102,11 @@ void system_mixer_music_fade_out(i32 ms) { inline void system_mixer_load_music(u8 *music_ptr, i32 music_size) { SDL_IOStream *rw = SDL_IOFromMem(music_ptr, music_size); + if (current_track != NULL) { + Mix_FreeMusic(current_track); + } current_track = Mix_LoadMUS_IO(rw, TRUE); + SDL_FlushIO(rw); } i32 system_mixer_free_music() { @@ -118,8 +126,9 @@ i32 system_mixer_play_music(u8 *music_ptr, i32 music_size, i32 loop) { i32 system_mixer_play_music_mp3(c8 *music_file) { int error_code = 0; current_track = Mix_LoadMUS(music_file); - if (current_track == NULL) + if (current_track == NULL) { printf("Mix_LoadMUS: %s\n", SDL_GetError()); + } error_code = Mix_PlayMusic(current_track, -1); if (error_code == -1) { printf("Mix_PlayMusic: %s\n", SDL_GetError());