changeset 1593:24508cb54f87

Fix a number of other memory errors (mostly leaks again) identified by valgrind
author Michael Pavone <pavone@retrodev.com>
date Thu, 28 Jun 2018 09:27:05 -0700
parents 31effaadf877
children 137dbd05ceab
files blastem.c config.c genesis.c m68k_core.c nuklear_ui/blastem_nuklear.c nuklear_ui/font.c render_sdl.c romdb.c
diffstat 8 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/blastem.c	Fri Jun 22 23:10:27 2018 -0700
+++ b/blastem.c	Thu Jun 28 09:27:05 2018 -0700
@@ -389,6 +389,7 @@
 	game_system->next_context = menu_system;
 	setup_saves(&cart, &info, game_system);
 	update_title(info.name);
+	free(info.name);
 }
 
 int main(int argc, char ** argv)
@@ -620,6 +621,7 @@
 	
 		setup_saves(&cart, &info, current_system);
 		update_title(info.name);
+		free(info.name);
 		if (menu) {
 			menu_system = current_system;
 		} else {
--- a/config.c	Fri Jun 22 23:10:27 2018 -0700
+++ b/config.c	Thu Jun 28 09:27:05 2018 -0700
@@ -198,6 +198,7 @@
 	uint32_t buf_size;
 	char *buffer = serialize_config(config, &buf_size);
 	uint8_t ret = buf_size == fwrite(buffer, 1, buf_size, f);
+	free(buffer);
 	fclose(f);
 	return ret;
 }
--- a/genesis.c	Fri Jun 22 23:10:27 2018 -0700
+++ b/genesis.c	Thu Jun 28 09:27:05 2018 -0700
@@ -1188,7 +1188,9 @@
 {
 	genesis_context *gen = (genesis_context *)system;
 	vdp_free(gen->vdp);
+	memmap_chunk *map = (memmap_chunk *)gen->m68k->options->gen.memmap;
 	m68k_options_free(gen->m68k->options);
+	free(map);//needs to happen after m68k_options_free as that function uses the memory map
 	free(gen->cart);
 	free(gen->m68k);
 	free(gen->work_ram);
--- a/m68k_core.c	Fri Jun 22 23:10:27 2018 -0700
+++ b/m68k_core.c	Thu Jun 28 09:27:05 2018 -0700
@@ -468,7 +468,11 @@
 		}
 	}
 	if (opts->num_movem == opts->movem_storage) {
-		opts->movem_storage *= 2;
+		if (!opts->movem_storage) {
+			opts->movem_storage = 4;
+		} else {
+			opts->movem_storage *= 2;
+		}
 		opts->big_movem = realloc(opts->big_movem, sizeof(movem_fun) * opts->movem_storage);
 	}
 	if (!opts->extra_code.cur) {
@@ -1205,6 +1209,7 @@
 		free(opts->gen.ram_inst_sizes[i]);
 	}
 	free(opts->gen.ram_inst_sizes);
+	free(opts->big_movem);
 	free(opts);
 }
 
--- a/nuklear_ui/blastem_nuklear.c	Fri Jun 22 23:10:27 2018 -0700
+++ b/nuklear_ui/blastem_nuklear.c	Thu Jun 28 09:27:05 2018 -0700
@@ -616,6 +616,7 @@
 		len = 11;
 	}
 	memcpy(buffer, curstr, len);
+	memset(buffer+len, 0, sizeof(buffer)-len);
 	nk_edit_string(context, NK_EDIT_SIMPLE, buffer, &len, sizeof(buffer)-1, nk_filter_decimal);
 	buffer[len] = 0;
 	if (strcmp(buffer, curstr)) {
@@ -1068,6 +1069,7 @@
 		fatal_error("Failed to find default font path\n");
 	}
 	def_font = nk_font_atlas_add_from_memory(atlas, font, font_size, render_height() / 16, NULL);
+	free(font);
 	nk_sdl_font_stash_end();
 	nk_style_set_font(context, &def_font->handle);
 	if (controller_360_buf) {
--- a/nuklear_ui/font.c	Fri Jun 22 23:10:27 2018 -0700
+++ b/nuklear_ui/font.c	Thu Jun 28 09:27:05 2018 -0700
@@ -35,6 +35,7 @@
 		goto error;
 	}
 	FILE *f = fopen(path, "rb");
+	free(path);
 	if (!f) {
 		goto error;
 	}
--- a/render_sdl.c	Fri Jun 22 23:10:27 2018 -0700
+++ b/render_sdl.c	Thu Jun 28 09:27:05 2018 -0700
@@ -541,6 +541,15 @@
 	un_height = glGetUniformLocation(program, "height");
 	at_pos = glGetAttribLocation(program, "pos");
 }
+
+static void gl_teardown()
+{
+	glDeleteProgram(program);
+	glDeleteShader(vshader);
+	glDeleteShader(fshader);
+	glDeleteBuffers(2, buffers);
+	glDeleteTextures(3, textures);
+}
 #endif
 
 static uint8_t texture_init;
@@ -588,6 +597,12 @@
 {
 	render_close_audio();
 	free_surfaces();
+#ifndef DISABLE_OPENGL
+	if (render_gl) {
+		gl_teardown();
+		SDL_GL_DeleteContext(main_context);
+	}
+#endif
 }
 
 static float config_aspect()
@@ -866,6 +881,7 @@
 				if (on_context_destroyed) {
 					on_context_destroyed();
 				}
+				gl_teardown();
 				SDL_GL_DeleteContext(main_context);
 				main_context = SDL_GL_CreateContext(main_window);
 				gl_setup();
@@ -1148,6 +1164,7 @@
 		if (on_context_destroyed) {
 			on_context_destroyed();
 		}
+		gl_teardown();
 		SDL_GL_DeleteContext(main_context);
 	} else {
 #endif
--- a/romdb.c	Fri Jun 22 23:10:27 2018 -0700
+++ b/romdb.c	Thu Jun 28 09:27:05 2018 -0700
@@ -608,6 +608,7 @@
 		}
 		if (matching_chunks == 0) {
 			//Nothing mapped in the relevant range for the lock-on cart, ignore this mapping
+			free_rom_info(&lock_info);
 			return;
 		} else if (matching_chunks > 1) {
 			state->info->map_chunks += matching_chunks - 1;