From 7ce645cfe6fffef55eb43e1e2b63e4ac41c8581c Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 18 Sep 2016 15:13:52 -0400 Subject: Fix memory, file, and library leaks --- src/instance.c | 1 + src/lib.c | 1 + src/plugin.c | 3 +++ src/state.c | 13 ++++++++----- src/util.c | 1 + test/lilv_test.c | 4 ++++ 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/instance.c b/src/instance.c index 9f27fa6..d05defd 100644 --- a/src/instance.c +++ b/src/instance.c @@ -78,6 +78,7 @@ lilv_plugin_instantiate(const LilvPlugin* plugin, if (result->lv2_handle == NULL) { // Failed to instantiate free(result); + lilv_lib_close(lib); return NULL; } diff --git a/src/lib.c b/src/lib.c index 8470684..f828d4f 100644 --- a/src/lib.c +++ b/src/lib.c @@ -57,6 +57,7 @@ lilv_lib_open(LilvWorld* world, desc = ldf(bundle_path, features); if (!desc) { LILV_ERRORF("Call to %s:lv2_lib_descriptor failed\n", lib_path); + dlclose(lib); lilv_free(lib_path); return NULL; } diff --git a/src/plugin.c b/src/plugin.c index f8ab26a..5e10d7c 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -210,6 +210,8 @@ lilv_plugin_load(LilvPlugin* p) if (st > SERD_FAILURE) { p->loaded = true; p->parse_errors = true; + serd_reader_free(reader); + serd_env_free(env); return; } @@ -287,6 +289,7 @@ lilv_plugin_load_ports_if_necessary(const LilvPlugin* const_p) lilv_node_as_uri(p->plugin_uri), lilv_node_as_string(symbol)); lilv_node_free(symbol); + lilv_node_free(index); lilv_plugin_free_ports(p); break; } diff --git a/src/state.c b/src/state.c index b852a31..7a8abc0 100644 --- a/src/state.c +++ b/src/state.c @@ -245,6 +245,7 @@ abstract_path(LV2_State_Map_Path_Handle handle, char* copy = lilv_get_latest_copy(real_path, cpath); if (!copy || !lilv_file_equals(real_path, copy)) { // No recent enough copy, make a new one + free(copy); copy = lilv_find_free_path(cpath, lilv_path_exists, NULL); const int st = lilv_copy_file(real_path, copy); if (st) { @@ -1204,11 +1205,13 @@ lilv_state_delete(LilvWorld* world, } static void -free_property_array(PropertyArray* array) +free_property_array(LilvState* state, PropertyArray* array) { for (uint32_t i = 0; i < array->n; ++i) { - if (array->props[i].flags & LV2_STATE_IS_POD) { - free(array->props[i].value); + Property* prop = &array->props[i]; + if ((prop->flags & LV2_STATE_IS_POD) || + prop->type == state->atom_Path) { + free(prop->value); } } free(array->props); @@ -1218,8 +1221,8 @@ LILV_API void lilv_state_free(LilvState* state) { if (state) { - free_property_array(&state->props); - free_property_array(&state->metadata); + free_property_array(state, &state->props); + free_property_array(state, &state->metadata); for (uint32_t i = 0; i < state->n_values; ++i) { free(state->values[i].value); free(state->values[i].symbol); diff --git a/src/util.c b/src/util.c index a0019cf..864bcbb 100644 --- a/src/util.c +++ b/src/util.c @@ -299,6 +299,7 @@ lilv_copy_file(const char* src, const char* dst) FILE* out = fopen(dst, "w"); if (!out) { + fclose(in); return errno; } diff --git a/test/lilv_test.c b/test/lilv_test.c index 7c7e06d..106f5c8 100644 --- a/test/lilv_test.c +++ b/test/lilv_test.c @@ -2073,6 +2073,7 @@ test_reload_bundle(void) // Load new bundle again (noop) lilv_world_load_bundle(world, bundle_uri); + cleanup_uris(); lilv_node_free(bundle_uri); lilv_world_free(world); world = NULL; @@ -2203,7 +2204,10 @@ test_get_symbol(void) lilv_node_free(frag); lilv_node_free(query_sym); lilv_node_free(query); + lilv_node_free(path_sym); + lilv_node_free(path); lilv_node_free(plug_sym); + cleanup_uris(); return 1; } -- cgit v1.2.1