Added proper cleanup of images and sounds to avoid segfaults

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-11 20:40:10 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-11 20:40:10 +0200
Commit a5fd60e79923c7c752a57e047150eaa3cdc8d229 (patch)
-rw-r--r-- main.c 48
1 files changed, 38 insertions, 10 deletions
diff --git a/main.c b/main.c
...
84
	list->count = 0;
84
	list->count = 0;
85
}
85
}
86
  
86
  
  
87
void free_image_list(ImageList *list) {
  
88
	ExternalImage *curr = list->head;
  
89
	while (curr) {
  
90
		ExternalImage *next = curr->next;
  
91
		UnloadTexture(curr->texture);
  
92
		free(curr);
  
93
		curr = next;
  
94
	}
  
95
	list->head = list->tail = NULL;
  
96
	list->count = 0;
  
97
}
  
98
  
  
99
void free_sound_list(SoundList *list) {
  
100
	ExternalSound *curr = list->head;
  
101
	while (curr) {
  
102
		ExternalSound *next = curr->next;
  
103
		UnloadSound(curr->sound);
  
104
		free(curr);
  
105
		curr = next;
  
106
	}
  
107
	list->head = list->tail = NULL;
  
108
	list->count = 0;
  
109
}
  
110
  
87
static int lua_getfield_int(lua_State *L, int index, const char *key) {
111
static int lua_getfield_int(lua_State *L, int index, const char *key) {
88
	lua_getfield(L, index, key);
112
	lua_getfield(L, index, key);
89
	int val = (int)luaL_checknumber(L, -1);
113
	int val = (int)luaL_checknumber(L, -1);
...
129
  
153
  
130
	InitAudioDevice();
154
	InitAudioDevice();
131
  
155
  
  
156
	init_image_list(&ctx.images);
  
157
	init_sound_list(&ctx.sounds);
  
158
  
132
	return 0;
159
	return 0;
133
}
160
}
134
  
161
  
135
// TODO: This function name is still a bit sus. Revisit the name later.
162
static int l_close_window(lua_State *L) {
  
163
	free_image_list(&ctx.images);
  
164
	free_sound_list(&ctx.sounds);
  
165
	UnloadFont(ctx.font);
  
166
	CloseAudioDevice();
  
167
	CloseWindow();
  
168
	return 0;
  
169
}
  
170
  
  
171
// XXX: This function name is still a bit sus. Revisit the name later.
136
static int l_window_running(lua_State *L) {
172
static int l_window_running(lua_State *L) {
137
	lua_pushboolean(L, !WindowShouldClose());
173
	lua_pushboolean(L, !WindowShouldClose());
138
	return 1;
174
	return 1;
...
162
static int l_get_height(lua_State *L) {
198
static int l_get_height(lua_State *L) {
163
	lua_pushnumber(L, GetScreenHeight());
199
	lua_pushnumber(L, GetScreenHeight());
164
	return 1;
200
	return 1;
165
}
  
166
  
  
167
static int l_close_window(lua_State *L) {
  
168
	CloseWindow();
  
169
	return 0;
  
170
}
201
}
171
  
202
  
172
static int l_start_drawing(lua_State *L) {
203
static int l_start_drawing(lua_State *L) {
...
544
  
575
  
545
int main(int argc, char *argv[]) {
576
int main(int argc, char *argv[]) {
546
	srand(time(NULL));
577
	srand(time(NULL));
547
	init_image_list(&ctx.images);
  
548
	init_sound_list(&ctx.sounds);
  
549
  
578
  
550
	TraceLogLevel debug_level = LOG_WARNING;
579
	TraceLogLevel debug_level = LOG_WARNING;
551
	const char *run_file = NULL;
580
	const char *run_file = NULL;
...
626
		lua_register(L, "draw_circle", l_draw_circle);
655
		lua_register(L, "draw_circle", l_draw_circle);
627
		lua_register(L, "draw_ellipse", l_draw_ellipse);
656
		lua_register(L, "draw_ellipse", l_draw_ellipse);
628
		lua_register(L, "draw_triangle", l_draw_triangle);
657
		lua_register(L, "draw_triangle", l_draw_triangle);
629
		
658
  
630
		lua_register(L, "load_image", l_load_image);
659
		lua_register(L, "load_image", l_load_image);
631
		lua_register(L, "draw_image", l_draw_image);
660
		lua_register(L, "draw_image", l_draw_image);
632
  
661
  
...
640
			return 1;
669
			return 1;
641
		}
670
		}
642
  
671
  
643
		UnloadFont(ctx.font);
  
644
		lua_close(L);
672
		lua_close(L);
645
	}
673
	}
646
  
674
  
...