Cleanup function for the stages array

This commit is contained in:
Andrei "Akari" Alexeyev 2017-02-15 14:53:24 +02:00 committed by Martin Herkt
parent de52c87af7
commit b88a6b0b42
3 changed files with 14 additions and 2 deletions

View file

@ -46,6 +46,7 @@ void taisei_shutdown(void) {
free_resources();
video_shutdown();
gamepad_shutdown();
stage_free_array();
config_uninit();
SDL_Quit();

View file

@ -215,7 +215,6 @@ void stage_init_array(void) {
if(stages[i].type == STAGE_SPELL) {
char *s, *postfix = difficulty_name(stages[i].difficulty);
// note: never free()'d
stages[i].subtitle = s = malloc(strlen(postfix) + strlen(stages[i].spell->name) + 4);
strcpy(s, stages[i].spell->name);
strcat(s, " ~ ");
@ -236,6 +235,18 @@ void stage_init_array(void) {
}
}
void stage_free_array(void) {
for(StageInfo *stg = stages; stg->loop; ++stg) {
if(stg->type == STAGE_SPELL) {
free(stg->subtitle);
}
if(stg->progress) {
free(stg->progress);
}
}
}
// NOTE: This returns the stage BY ID, not by the array index!
StageInfo* stage_get(uint16_t n) {
for(StageInfo *stg = stages; stg->loop; ++stg)
@ -259,7 +270,6 @@ StageProgress* stage_get_progress_from_info(StageInfo *stage, Difficulty diff, b
// D_Any stages will have a separate StageProgress for every selectable difficulty.
// Stages with a fixed difficulty setting (spellpractice, extra stage...) obviously get just one and the diff parameter is ignored.
// We never free anything we allocate here either. RAM's cheap right?
// This stuff must stay around until progress_save(), which happens on shutdown.
// So do NOT try to free any pointers this function returns, that will fuck everything up.

View file

@ -84,6 +84,7 @@ StageProgress* stage_get_progress(uint16_t id, Difficulty diff, bool allocate);
StageProgress* stage_get_progress_from_info(StageInfo *stage, Difficulty diff, bool allocate);
void stage_init_array(void);
void stage_free_array(void);
void stage_loop(StageRule start, StageRule end, StageRule draw, StageRule event, ShaderRule *shaderrules, int endtime);