🐞 fix(utable): fix memleak in test case
This commit is contained in:
@@ -676,15 +676,16 @@ cJSON *ipfix_init_utable_from_log_json(struct utable *table, const char *test_js
|
||||
|
||||
cJSON *json_array = cJSON_CreateIntArray((const int *)array, array_sz);
|
||||
cJSON_Delete(item->child);
|
||||
item->type = cJSON_String;
|
||||
item->child = NULL;
|
||||
|
||||
item->type = cJSON_String;
|
||||
item->valuestring = cJSON_PrintUnformatted(json_array);
|
||||
cJSON_Delete(json_array);
|
||||
|
||||
free(array);
|
||||
free(long_array);
|
||||
}
|
||||
if (array_item->type == cJSON_String)
|
||||
else if (array_item->type == cJSON_String)
|
||||
{
|
||||
char **array = (char **)malloc(sizeof(char *) * array_sz);
|
||||
size_t *sz_value = (size_t *)malloc(sizeof(size_t) * array_sz);
|
||||
@@ -698,7 +699,7 @@ cJSON *ipfix_init_utable_from_log_json(struct utable *table, const char *test_js
|
||||
}
|
||||
}
|
||||
utable_add_cstring_array(table, item->string, (const char **)array, sz_value, array_sz);
|
||||
|
||||
free(sz_value);
|
||||
cJSON *json_array = cJSON_CreateStringArray((const char **)array, array_sz);
|
||||
item->valuestring = cJSON_PrintUnformatted(json_array);
|
||||
item->type = cJSON_String;
|
||||
|
||||
4
deps/utable/test/unit_test_utable.cpp
vendored
4
deps/utable/test/unit_test_utable.cpp
vendored
@@ -601,7 +601,7 @@ TEST(utable_test, replace_8k_cstring)
|
||||
utable_free(table);
|
||||
}
|
||||
|
||||
TEST(utable_test, replace_cstring_many_times)
|
||||
TEST(utable_test, replace_cstring_1w_times)
|
||||
{
|
||||
struct utable *table = utable_new();
|
||||
|
||||
@@ -614,7 +614,7 @@ TEST(utable_test, replace_cstring_many_times)
|
||||
|
||||
test_utable_assert_str(table, "key1", str);
|
||||
|
||||
for(int i=0; i<100000; i++)
|
||||
for(int i=0; i<10000; i++)
|
||||
{
|
||||
utable_delete(table, "key1", strlen("key1"));
|
||||
utable_add_cstring(table, "key1", str, strlen(str));
|
||||
|
||||
12
deps/utable/utable.c
vendored
12
deps/utable/utable.c
vendored
@@ -347,7 +347,7 @@ void utable_add_cstring(struct utable *table, const char *key, const char *value
|
||||
return;
|
||||
}
|
||||
item = MEMPOOL_ALLOC(table->mempool ,1,struct utable_item);
|
||||
item->kv= utable_kv_new_with_cstring(key, key_sz, value, value_sz);
|
||||
item->kv= utable_kv_new_with_cstring_from_mempool(table->mempool, key, key_sz, value, value_sz);
|
||||
HASH_ADD_KEYPTR(hh, table->items, item->kv->key, item->kv->key_sz, item);
|
||||
utable_item_stat_add(&table->stat, item->kv);
|
||||
}
|
||||
@@ -363,7 +363,7 @@ void utable_add_blob(struct utable *table, const char *key, const char *blob, si
|
||||
return;
|
||||
}
|
||||
item = MEMPOOL_ALLOC(table->mempool ,1, struct utable_item);
|
||||
item->kv = utable_kv_new_with_blob(key, key_sz, blob, blob_sz);
|
||||
item->kv = utable_kv_new_with_blob_from_mempool(table->mempool, key, key_sz, blob, blob_sz);
|
||||
|
||||
HASH_ADD_KEYPTR(hh, table->items, item->kv->key, item->kv->key_sz, item);
|
||||
utable_item_stat_add(&table->stat, item->kv);
|
||||
@@ -380,7 +380,7 @@ void utable_add_integer(struct utable *table, const char *key, int64_t value)
|
||||
return;
|
||||
}
|
||||
item = MEMPOOL_ALLOC(table->mempool ,1, struct utable_item);
|
||||
item->kv = utable_kv_new_with_integer(key, key_sz, value);
|
||||
item->kv = utable_kv_new_with_integer_from_mempool(table->mempool, key, key_sz, value);
|
||||
|
||||
HASH_ADD_KEYPTR(hh, table->items, item->kv->key, item->kv->key_sz, item);
|
||||
utable_item_stat_add(&table->stat, item->kv);
|
||||
@@ -397,7 +397,7 @@ void utable_add_integer_array(struct utable *table, const char *key, int64_t val
|
||||
return;
|
||||
}
|
||||
item = MEMPOOL_ALLOC(table->mempool ,1, struct utable_item);
|
||||
item->kv = utable_kv_new_with_integer_array(key, key_sz, value_array, n_value);
|
||||
item->kv = utable_kv_new_with_integer_array_from_mempool(table->mempool, key, key_sz, value_array, n_value);
|
||||
|
||||
HASH_ADD_KEYPTR(hh, table->items, item->kv->key, item->kv->key_sz, item);
|
||||
utable_item_stat_add(&table->stat, item->kv);
|
||||
@@ -414,7 +414,7 @@ void utable_add_cstring_array(struct utable *table, const char *key, const char*
|
||||
return;
|
||||
}
|
||||
item = MEMPOOL_ALLOC(table->mempool ,1, struct utable_item);
|
||||
item->kv = utable_kv_new_with_cstring_array(key, key_sz, value_array, value_sz, n_value);
|
||||
item->kv = utable_kv_new_with_cstring_array_from_mempool(table->mempool, key, key_sz, value_array, value_sz, n_value);
|
||||
|
||||
HASH_ADD_KEYPTR(hh, table->items, item->kv->key, item->kv->key_sz, item);
|
||||
utable_item_stat_add(&table->stat, item->kv);
|
||||
@@ -427,7 +427,7 @@ void utable_delete(struct utable *table, const char *key, size_t key_sz)
|
||||
if (item) {
|
||||
HASH_DEL(table->items, item);
|
||||
utable_item_stat_sub(&table->stat, item->kv);
|
||||
utable_kv_free(item->kv);
|
||||
utable_kv_free_from_pool(table->mempool, item->kv);
|
||||
MEMPOOL_FREE(table->mempool,item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +70,10 @@ struct module_manager *module_manager_new_with_toml(const char *toml_path, int m
|
||||
struct module_hooks mod_hooks[mod_num];
|
||||
memset(mod_hooks, 0, sizeof(mod_hooks));
|
||||
|
||||
char *path = NULL;
|
||||
char *instance_init_cb_name = NULL, *instance_exit_cb_name = NULL, *thread_init_cb_name = NULL,
|
||||
*thread_exit_cb_name = NULL;
|
||||
|
||||
for (int i = 0; i < mod_num; i++)
|
||||
{
|
||||
toml_table_t* toml_mod = toml_table_at(mod_array, i);
|
||||
@@ -80,21 +84,15 @@ struct module_manager *module_manager_new_with_toml(const char *toml_path, int m
|
||||
const char *thread_init_func_name_raw = toml_raw_in(toml_mod, "thread_init");
|
||||
const char *thread_exit_func_name_raw = toml_raw_in(toml_mod, "thread_exit");
|
||||
|
||||
char *path = NULL;
|
||||
char *instance_init_cb_name = NULL, *instance_exit_cb_name = NULL, *thread_init_cb_name = NULL,
|
||||
*thread_exit_cb_name = NULL;
|
||||
|
||||
toml_rtos(path_raw, &path);
|
||||
toml_rtos(init_func_name_raw, &instance_init_cb_name);
|
||||
toml_rtos(exit_func_name_raw, &instance_exit_cb_name);
|
||||
toml_rtos(thread_init_func_name_raw, &thread_init_cb_name);
|
||||
toml_rtos(thread_exit_func_name_raw, &thread_exit_cb_name);
|
||||
|
||||
void* handle = dlopen(path, RTLD_NOW|RTLD_LAZY|RTLD_GLOBAL);
|
||||
if (!handle) {
|
||||
fprintf(stderr, "Error loading module %s: %s\n", path, dlerror());
|
||||
if(path)FREE(path);
|
||||
if (!handle)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
toml_rtos(init_func_name_raw, &instance_init_cb_name);
|
||||
if (instance_init_cb_name)
|
||||
{
|
||||
mod_hooks[i].on_instance_init_cb =
|
||||
@@ -103,6 +101,8 @@ struct module_manager *module_manager_new_with_toml(const char *toml_path, int m
|
||||
|
||||
FREE(instance_init_cb_name);
|
||||
}
|
||||
|
||||
toml_rtos(exit_func_name_raw, &instance_exit_cb_name);
|
||||
if (instance_exit_cb_name)
|
||||
{
|
||||
mod_hooks[i].on_instance_exit_cb =
|
||||
@@ -110,6 +110,7 @@ struct module_manager *module_manager_new_with_toml(const char *toml_path, int m
|
||||
FREE(instance_exit_cb_name);
|
||||
}
|
||||
|
||||
toml_rtos(thread_init_func_name_raw, &thread_init_cb_name);
|
||||
if (thread_init_cb_name)
|
||||
{
|
||||
mod_hooks[i].on_thread_init_cb =
|
||||
@@ -117,6 +118,7 @@ struct module_manager *module_manager_new_with_toml(const char *toml_path, int m
|
||||
FREE(thread_init_cb_name);
|
||||
}
|
||||
|
||||
toml_rtos(thread_exit_func_name_raw, &thread_exit_cb_name);
|
||||
if (thread_exit_cb_name)
|
||||
{
|
||||
mod_hooks[i].on_thread_exit_cb =
|
||||
@@ -146,6 +148,12 @@ void module_manager_free(struct module_manager *mod_mgr)
|
||||
}
|
||||
FREE(mod_mgr->descriptors);
|
||||
}
|
||||
struct polling_node *node, *tmp;
|
||||
LL_FOREACH_SAFE(mod_mgr->node_list, node, tmp)
|
||||
{
|
||||
LL_DELETE(mod_mgr->node_list, node);
|
||||
FREE(node);
|
||||
}
|
||||
FREE(mod_mgr);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user