Give back every pooled string and sprite reference the loaders take
Closes Defects items 20, 22, 23 and 29, and the residual of item 38. The tilemap load leak was five pooled strings per load; the property-lookup fix in an earlier commit took it to two, and the last two were each a claim with no matching release -- the string every layer's `type` was read into, and the dirname the map's relative paths resolve against. tests/tilemap.c asserts the pool is exactly where it started after one load/release cycle and after 64. Finding those two was a matter of dumping the contents of every still-claimed slot after a cycle rather than reading the code again; 'tilelayer' and an assets directory named themselves immediately. Same file, same class, fixed with it: akgl_tilemap_load_layer_objects released its scratch string after reading each object's name and then kept using the slot, because akgl_get_json_string_value reuses a non-NULL destination without taking another reference. The slot was free while still live, so any other claim could have been handed it. akgl_character_sprite_add wrote over an existing binding without releasing the sprite it displaced, so a character that rebinds a state while alive leaked a sprite slot per rebind -- teardown only gives back what the map holds at the end. The new reference is taken before the write and given back if the write fails, so there is no window where a sprite is bound with nothing behind it. The write was unchecked too. Three failure-path leaks moved into CLEANUP blocks: akgl_render_2d_init's two pooled strings, akgl_controller_open_gamepads' enumeration array, and akgl_text_rendertextat's surface and texture -- the last being a leak per frame on a HUD line. akgl_text_unloadallfonts() closes every font in the registry and destroys it, which is what item 38 left open. Deliberately not a whole akgl_game_shutdown: tearing down the mixer, SDL_ttf and SDL in the right order is a design question, and this is the part that was simply missing. It is a new public symbol, which 0.5.0 already covers -- this release has not shipped. Every fix has a test that fails against the old code. 25/25 pass, memcheck clean, reindent --check, check_api_surface and check_error_protocol all clean. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include <akgl/error.h>
|
||||
#include <akgl/game.h>
|
||||
#include <akgl/renderer.h>
|
||||
#include <akgl/registry.h>
|
||||
|
||||
#include "testutil.h"
|
||||
|
||||
@@ -187,6 +188,58 @@ akerr_ErrorContext *test_render_unimplemented_and_guards(void)
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief akgl_render_2d_init must give back its pooled strings when a parse fails.
|
||||
*
|
||||
* It reads game.screenwidth and game.screenheight into two pool strings and
|
||||
* used to release them only after both had parsed, so a non-numeric value
|
||||
* returned past both and leaked two of the pool's 256 entries -- every time a
|
||||
* host started with a bad configuration and retried.
|
||||
*
|
||||
* The window creation after the parse is what makes the success path need a
|
||||
* display, so this only drives the failure path. That is the one that leaked.
|
||||
*/
|
||||
akerr_ErrorContext *test_render_2d_init_releases_strings_on_failure(void)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akgl_RenderBackend backend;
|
||||
int baseline = 0;
|
||||
int i = 0;
|
||||
|
||||
ATTEMPT {
|
||||
CATCH(errctx, akgl_registry_init_properties());
|
||||
memset(&backend, 0x00, sizeof(akgl_RenderBackend));
|
||||
|
||||
CATCH(errctx, akgl_set_property("game.screenwidth", "not-a-number"));
|
||||
CATCH(errctx, akgl_set_property("game.screenheight", "480"));
|
||||
|
||||
baseline = test_string_pool_used();
|
||||
for ( i = 0; i < (AKGL_MAX_HEAP_STRING * 2); i++ ) {
|
||||
TEST_EXPECT_ANY_ERROR(errctx, akgl_render_2d_init(&backend),
|
||||
"initializing a 2D renderer from an unparseable width");
|
||||
}
|
||||
TEST_ASSERT(errctx, test_string_pool_used() == baseline,
|
||||
"%d failed initializations left %d pool strings claimed, expected %d",
|
||||
(AKGL_MAX_HEAP_STRING * 2), test_string_pool_used(), baseline);
|
||||
|
||||
// The second property is the one read after the first parse, so fail on
|
||||
// it too and check the other order.
|
||||
CATCH(errctx, akgl_set_property("game.screenwidth", "640"));
|
||||
CATCH(errctx, akgl_set_property("game.screenheight", "also-not-a-number"));
|
||||
baseline = test_string_pool_used();
|
||||
for ( i = 0; i < (AKGL_MAX_HEAP_STRING * 2); i++ ) {
|
||||
TEST_EXPECT_ANY_ERROR(errctx, akgl_render_2d_init(&backend),
|
||||
"initializing a 2D renderer from an unparseable height");
|
||||
}
|
||||
TEST_ASSERT(errctx, test_string_pool_used() == baseline,
|
||||
"%d failed initializations left %d pool strings claimed, expected %d",
|
||||
(AKGL_MAX_HEAP_STRING * 2), test_string_pool_used(), baseline);
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
@@ -226,6 +279,7 @@ int main(void)
|
||||
CATCH(errctx, test_render_backend_without_a_renderer());
|
||||
CATCH(errctx, test_render_frames_and_textures());
|
||||
CATCH(errctx, test_render_unimplemented_and_guards());
|
||||
CATCH(errctx, test_render_2d_init_releases_strings_on_failure());
|
||||
} CLEANUP {
|
||||
SDL_Quit();
|
||||
} PROCESS(errctx) {
|
||||
|
||||
Reference in New Issue
Block a user