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:
@@ -322,41 +322,58 @@ akerr_ErrorContext *akgl_tilemap_load_layer_objects(akgl_Tilemap *dest, json_t *
|
||||
PASS(errctx, akgl_get_json_array_value(root, "objects", &layerdata));
|
||||
len = json_array_size((json_t *)layerdata);
|
||||
curlayer = &dest->layers[layerid];
|
||||
for ( j = 0; j < len; j++ ) {
|
||||
FAIL_NONZERO_RETURN(
|
||||
errctx,
|
||||
(j >= AKGL_TILEMAP_MAX_OBJECTS_PER_LAYER),
|
||||
AKERR_OUTOFBOUNDS,
|
||||
"Object layer %d has more than %d objects",
|
||||
layerid,
|
||||
AKGL_TILEMAP_MAX_OBJECTS_PER_LAYER
|
||||
);
|
||||
PASS(errctx, akgl_get_json_array_index_object((json_t *)layerdata, j, &layerdatavalue));
|
||||
curobj = &curlayer->objects[j];
|
||||
PASS(errctx, akgl_get_json_string_value((json_t *)layerdatavalue, "name", &tmpstr));
|
||||
strncpy((char *)curobj->name, tmpstr->data, AKGL_ACTOR_MAX_NAME_LENGTH);
|
||||
PASS(errctx, akgl_heap_release_string(tmpstr));
|
||||
PASS(errctx, akgl_get_json_number_value((json_t *)layerdatavalue, "x", &curobj->x));
|
||||
PASS(errctx, akgl_get_json_number_value((json_t *)layerdatavalue, "y", &curobj->y));
|
||||
PASS(errctx, akgl_get_json_boolean_value((json_t *)layerdatavalue, "visible", &curobj->visible));
|
||||
|
||||
PASS(errctx, akgl_get_json_string_value((json_t *)layerdatavalue, "type", &tmpstr));
|
||||
if ( strcmp(tmpstr->data, "actor") == 0 ) {
|
||||
PASS(errctx, akgl_tilemap_load_layer_object_actor(curobj, layerdatavalue, layerid, dirname));
|
||||
} else if ( strcmp(tmpstr->data, "perspective") == 0 ) {
|
||||
curobj->visible = false;
|
||||
if ( strcmp((char *)curobj->name, "p_foreground") == 0 ) {
|
||||
dest->p_foreground_y = curobj->y;
|
||||
PASS(errctx, akgl_get_json_integer_value((json_t *)layerdatavalue, "height", &dest->p_foreground_h));
|
||||
PASS(errctx, akgl_get_json_properties_float((json_t *)layerdatavalue, "scale", &dest->p_foreground_scale));
|
||||
} else if ( strcmp((char *)curobj->name, "p_vanishing") == 0 ) {
|
||||
dest->p_vanishing_y = curobj->y;
|
||||
PASS(errctx, akgl_get_json_integer_value((json_t *)layerdatavalue, "height", &dest->p_vanishing_h));
|
||||
PASS(errctx, akgl_get_json_properties_float((json_t *)layerdatavalue, "scale", &dest->p_vanishing_scale));
|
||||
// The loop is the whole ATTEMPT body on purpose. A CATCH inside a loop
|
||||
// breaks the loop rather than the block, which is only safe because there
|
||||
// is nothing after it -- CLEANUP, PROCESS and FINISH still run and still
|
||||
// propagate. Anything added after this loop has to account for that.
|
||||
ATTEMPT {
|
||||
for ( j = 0; j < len; j++ ) {
|
||||
FAIL_NONZERO_BREAK(
|
||||
errctx,
|
||||
(j >= AKGL_TILEMAP_MAX_OBJECTS_PER_LAYER),
|
||||
AKERR_OUTOFBOUNDS,
|
||||
"Object layer %d has more than %d objects",
|
||||
layerid,
|
||||
AKGL_TILEMAP_MAX_OBJECTS_PER_LAYER
|
||||
);
|
||||
CATCH(errctx, akgl_get_json_array_index_object((json_t *)layerdata, j, &layerdatavalue));
|
||||
curobj = &curlayer->objects[j];
|
||||
// One scratch string for the whole walk, released once in CLEANUP.
|
||||
// Releasing it after the name read -- which is what this used to do
|
||||
// -- dropped its refcount to zero while the very next line went on
|
||||
// using the same slot, because akgl_get_json_string_value reuses a
|
||||
// non-NULL destination without taking another reference. Any other
|
||||
// claim in between would have been handed the same string.
|
||||
CATCH(errctx, akgl_get_json_string_value((json_t *)layerdatavalue, "name", &tmpstr));
|
||||
strncpy((char *)curobj->name, tmpstr->data, AKGL_ACTOR_MAX_NAME_LENGTH);
|
||||
CATCH(errctx, akgl_get_json_number_value((json_t *)layerdatavalue, "x", &curobj->x));
|
||||
CATCH(errctx, akgl_get_json_number_value((json_t *)layerdatavalue, "y", &curobj->y));
|
||||
CATCH(errctx, akgl_get_json_boolean_value((json_t *)layerdatavalue, "visible", &curobj->visible));
|
||||
|
||||
CATCH(errctx, akgl_get_json_string_value((json_t *)layerdatavalue, "type", &tmpstr));
|
||||
if ( strcmp(tmpstr->data, "actor") == 0 ) {
|
||||
CATCH(errctx, akgl_tilemap_load_layer_object_actor(curobj, layerdatavalue, layerid, dirname));
|
||||
} else if ( strcmp(tmpstr->data, "perspective") == 0 ) {
|
||||
curobj->visible = false;
|
||||
if ( strcmp((char *)curobj->name, "p_foreground") == 0 ) {
|
||||
dest->p_foreground_y = curobj->y;
|
||||
CATCH(errctx, akgl_get_json_integer_value((json_t *)layerdatavalue, "height", &dest->p_foreground_h));
|
||||
CATCH(errctx, akgl_get_json_properties_float((json_t *)layerdatavalue, "scale", &dest->p_foreground_scale));
|
||||
} else if ( strcmp((char *)curobj->name, "p_vanishing") == 0 ) {
|
||||
dest->p_vanishing_y = curobj->y;
|
||||
CATCH(errctx, akgl_get_json_integer_value((json_t *)layerdatavalue, "height", &dest->p_vanishing_h));
|
||||
CATCH(errctx, akgl_get_json_properties_float((json_t *)layerdatavalue, "scale", &dest->p_vanishing_scale));
|
||||
}
|
||||
}
|
||||
layerdatavalue = NULL;
|
||||
}
|
||||
layerdatavalue = NULL;
|
||||
}
|
||||
} CLEANUP {
|
||||
if ( tmpstr != NULL ) {
|
||||
IGNORE(akgl_heap_release_string(tmpstr));
|
||||
}
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -471,6 +488,12 @@ akerr_ErrorContext *akgl_tilemap_load_layers(akgl_Tilemap *dest, json_t *root, a
|
||||
layerid += 1;
|
||||
}
|
||||
} CLEANUP {
|
||||
// One scratch string for every layer's type, given back once. It was
|
||||
// claimed by the first accessor call and never released, which is one
|
||||
// of the two pool strings a map load used to keep for good.
|
||||
if ( tmpstr != NULL ) {
|
||||
IGNORE(akgl_heap_release_string(tmpstr));
|
||||
}
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
SUCCEED_RETURN(errctx);
|
||||
@@ -618,7 +641,6 @@ akerr_ErrorContext *akgl_tilemap_load(char *fname, akgl_Tilemap *dest)
|
||||
SDL_Log("Map perspective rate is %f", dest->p_rate);
|
||||
}
|
||||
} CLEANUP {
|
||||
//IGNORE(akgl_heap_release_string(tmpstr));
|
||||
// The map is built entirely out of copies -- layer data, tileset
|
||||
// geometry, object names -- so the document is dead the moment the
|
||||
// loaders above return, whether they succeeded or not. It is also the
|
||||
@@ -627,6 +649,13 @@ akerr_ErrorContext *akgl_tilemap_load(char *fname, akgl_Tilemap *dest)
|
||||
json_decref(json);
|
||||
json = NULL;
|
||||
}
|
||||
// And the directory the map's relative paths resolve against, which
|
||||
// was claimed above and never released -- the other of the two pool
|
||||
// strings a map load kept.
|
||||
if ( dirnamestr != NULL ) {
|
||||
IGNORE(akgl_heap_release_string(dirnamestr));
|
||||
dirnamestr = NULL;
|
||||
}
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
SUCCEED_RETURN(errctx);
|
||||
|
||||
Reference in New Issue
Block a user