# HG changeset patch # User Michael Pavone # Date 1529734227 25200 # Node ID 31effaadf877bda589be87c5305296cb315fd023 # Parent 5cfc7e4a207ea9ef36dc2d1be51fe3c7d59fcefc Fix some memory errors (mostly leaks) identified by valgrind diff -r 5cfc7e4a207e -r 31effaadf877 blastem.c --- a/blastem.c Fri Jun 22 21:11:38 2018 -0700 +++ b/blastem.c Fri Jun 22 23:10:27 2018 -0700 @@ -259,7 +259,7 @@ //initial save dir was calculated based on lock-on cartridge because that's where the save device is //save directory used for save states should still be located in the normal place free(save_dir); - save_dir = get_save_dir(media); + parts[0] = save_dir = get_save_dir(media); } if (use_native_states || context->type != SYSTEM_GENESIS) { parts[2] = "quicksave.state"; diff -r 5cfc7e4a207e -r 31effaadf877 genesis.c --- a/genesis.c Fri Jun 22 21:11:38 2018 -0700 +++ b/genesis.c Fri Jun 22 23:10:27 2018 -0700 @@ -1301,7 +1301,6 @@ gen->psg = malloc(sizeof(psg_context)); psg_init(gen->psg, gen->master_clock, MCLKS_PER_PSG); - gen->zram = calloc(1, Z80_RAM_BYTES); z80_map[0].buffer = gen->zram = calloc(1, Z80_RAM_BYTES); #ifndef NO_Z80 z80_options *z_opts = malloc(sizeof(z80_options)); diff -r 5cfc7e4a207e -r 31effaadf877 m68k_core.c --- a/m68k_core.c Fri Jun 22 21:11:38 2018 -0700 +++ b/m68k_core.c Fri Jun 22 23:10:27 2018 -0700 @@ -1191,7 +1191,19 @@ void m68k_options_free(m68k_options *opts) { + for (uint32_t address = 0; address < opts->gen.address_mask; address += NATIVE_CHUNK_SIZE) + { + uint32_t chunk = address / NATIVE_CHUNK_SIZE; + if (opts->gen.native_code_map[chunk].base) { + free(opts->gen.native_code_map[chunk].offsets); + } + } free(opts->gen.native_code_map); + uint32_t ram_inst_slots = ram_size(&opts->gen) / 1024; + for (uint32_t i = 0; i < ram_inst_slots; i++) + { + free(opts->gen.ram_inst_sizes[i]); + } free(opts->gen.ram_inst_sizes); free(opts); } diff -r 5cfc7e4a207e -r 31effaadf877 png.c --- a/png.c Fri Jun 22 21:11:38 2018 -0700 +++ b/png.c Fri Jun 22 23:10:27 2018 -0700 @@ -430,6 +430,7 @@ } last_line = line_start; } + free(decomp_buffer); } else { //skip uncrecognized chunks cur += 4 + chunk_size; diff -r 5cfc7e4a207e -r 31effaadf877 romdb.c --- a/romdb.c Fri Jun 22 21:11:38 2018 -0700 +++ b/romdb.c Fri Jun 22 23:10:27 2018 -0700 @@ -51,6 +51,8 @@ free(info->save_buffer); if (info->save_type == SAVE_I2C) { free(info->eeprom_map); + } else if (info->save_type == SAVE_NOR) { + free(info->nor); } } free(info->map); @@ -58,7 +60,6 @@ free(info->port2_override); free(info->ext_override); free(info->mouse_mode); - free(info->nor); } void cart_serialize(system_header *sys, serialize_buffer *buf) diff -r 5cfc7e4a207e -r 31effaadf877 util.c --- a/util.c Fri Jun 22 21:11:38 2018 -0700 +++ b/util.c Fri Jun 22 23:10:27 2018 -0700 @@ -359,9 +359,11 @@ for (extidx = 0; extidx < num_exts; extidx++) { if (!strcasecmp(ext, ext_list[extidx])) { + free(ext); return 1; } } + free(ext); return 0; } @@ -721,6 +723,7 @@ if (numret) { *numret = pos; } + closedir(d); return ret; } diff -r 5cfc7e4a207e -r 31effaadf877 z80_to_x86.c --- a/z80_to_x86.c Fri Jun 22 21:11:38 2018 -0700 +++ b/z80_to_x86.c Fri Jun 22 23:10:27 2018 -0700 @@ -3671,7 +3671,19 @@ void z80_options_free(z80_options *opts) { + for (uint32_t address = 0; address < opts->gen.address_mask; address += NATIVE_CHUNK_SIZE) + { + uint32_t chunk = address / NATIVE_CHUNK_SIZE; + if (opts->gen.native_code_map[chunk].base) { + free(opts->gen.native_code_map[chunk].offsets); + } + } free(opts->gen.native_code_map); + uint32_t ram_inst_slots = ram_size(&opts->gen) / 1024; + for (uint32_t i = 0; i < ram_inst_slots; i++) + { + free(opts->gen.ram_inst_sizes[i]); + } free(opts->gen.ram_inst_sizes); free(opts); }