OCT 1. 修正代码内存泄漏

2. 增加程序退出时自动释放资源功能
This commit is contained in:
huangxin 2022-11-18 11:02:30 +08:00
parent 0b46222137
commit 42795a7361
6 changed files with 44 additions and 24 deletions

View File

@ -13,8 +13,9 @@
#define CRLF "\r\n"
KHASH_MAP_INIT_STR(string_hashmap, hw_string *)
static uv_timer_t cache_invalidation_timer;
//static uv_timer_t cache_invalidation_timer;
static uv_key_t thread_cache_key;
static uv_timer_t *cache_invalidation_timer;
void initialize_http_request_cache();
void free_http_request_cache();
@ -27,8 +28,14 @@ void initialize_http_request_cache() {
uv_key_create(&thread_cache_key);
}
void uninit_http_request_cache() {
if (cache_invalidation_timer) {
free(cache_invalidation_timer);
}
}
void http_request_cache_configure_listener(uv_loop_t *loop, uv_async_t *handle) {
uv_timer_t *cache_invalidation_timer = malloc(sizeof(uv_timer_t));
cache_invalidation_timer = malloc(sizeof(uv_timer_t));
uv_timer_init(loop, cache_invalidation_timer);
uv_timer_start(cache_invalidation_timer, http_request_cache_timer, 500, 500);

View File

@ -5,3 +5,4 @@
void initialize_http_request_cache();
hw_string *get_cached_request(const char *http_status);
void http_request_cache_configure_listener(uv_loop_t *loop, uv_async_t *handle);
void uninit_http_request_cache();

View File

@ -145,13 +145,14 @@ void free_http_server() {
free((char *)v);
});
kh_destroy(string_hashmap, routes);
uv_close((uv_handle_t*)&server, NULL);
uninit_http_request_cache();
dzlog_debug("HTTP Server Close http://%s:%d\n", config->http_listen_address, config->http_listen_port);
}
int hw_http_open() {
int threads = (int)config->thread_count;
uv_async_t *service_handle;
static uv_async_t service_handle;
if (routes == NULL) {
routes = kh_init(string_hashmap);
@ -180,8 +181,8 @@ int hw_http_open() {
listeners_created_barrier = malloc(sizeof(uv_barrier_t));
uv_barrier_init(listeners_created_barrier, listener_count + 1);
service_handle = malloc(sizeof(uv_async_t));
uv_async_init(uv_loop, service_handle, NULL);
//service_handle = malloc(sizeof(uv_async_t));
uv_async_init(uv_loop, &service_handle, NULL);
if (listener_count == 0) {
/* If running single threaded there is no need to use the IPC pipe

View File

@ -147,31 +147,31 @@ static const char *load_string_value(const char *pKeyName) {
}
}
static int load_boolean_value(const char *pKeyName, int defValue) {
static int load_boolean_value(const char *pKeyName) {
int val;
if (config_lookup_bool(&g_cfgContent, pKeyName, &val) != CONFIG_TRUE) {
dzlog_error("No {%s} setting in configuration file.\n", pKeyName);
return defValue;
return DEFAULT_INTEGRAL_ERR_VALUE;
}
return val;
}
static long long load_integral_value(const char *pKeyName, long long defValue) {
static long long int load_integral_value(const char *pKeyName) {
long long val;
if (config_lookup_int64(&g_cfgContent, pKeyName, &val) != CONFIG_TRUE) {
dzlog_error("No {%s} setting in configuration file.\n", pKeyName);
return defValue;
return DEFAULT_INTEGRAL_ERR_VALUE;
}
return val;
}
static double load_float_value(const char *pKeyName, double defValue) {
static double load_float_value(const char *pKeyName) {
double val;
if (config_lookup_float(&g_cfgContent, pKeyName, &val) != CONFIG_TRUE) {
dzlog_error("No {%s} setting in configuration file.\n", pKeyName);
return defValue;
return DEFAULT_INTEGRAL_ERR_VALUE;
}
return val;
@ -254,15 +254,10 @@ static int load_array_obj(const char *pKeyName, PCONFIG_ITEM pValue) {
v.lease = 0xFFFFFFFF;
}
if (pValue->value.array == NULL) {
pValue->value.array = vect_create(128, sizeof(OBJ_DHCP_RNG), ZV_SEC_WIPE);
}
if (!vect_bsearch(pValue->value.array, &v, cmp_dhcp_obj, &idx)) {
memcpy(&k, &v, sizeof(OBJ_DHCP_RNG));
vect_push(pValue->value.array, &k);
pValue->isChanged = TRUE;
printf("++++Add %s\n", k.rangAddr);
}
} break;
@ -304,8 +299,7 @@ static int setConfigItemValue(PCONFIG_ITEM pItem, const char *pValue) {
} else if (pItem->valType == VALUE_TYPE_ARRAY_STR) {
pItem->value.array = vect_create(128, 512, ZV_SEC_WIPE);
} else if (pItem->valType == VALUE_TYPE_ARRAY_OBJ) {
pItem->value.array = NULL;
vect_create(128, sizeof(int), ZV_SEC_WIPE | ZV_BYREF);
pItem->value.array = vect_create(128, sizeof(OBJ_DHCP_RNG), ZV_SEC_WIPE);
} else {
return -ERR_UN_SUPPORT;
}
@ -397,7 +391,7 @@ static void refreshCfgFileCb() {
}
break;
case VALUE_TYPE_BOOL:
iVal = load_boolean_value(pItem->pcfgKey, DEFAULT_INTEGRAL_ERR_VALUE);
iVal = load_boolean_value(pItem->pcfgKey);
if (iVal != DEFAULT_INTEGRAL_ERR_VALUE) {
if (pItem->value.longValue != iVal) {
@ -408,7 +402,7 @@ static void refreshCfgFileCb() {
break;
case VALUE_TYPE_INTEGRAL:
iVal = load_integral_value(pItem->pcfgKey, DEFAULT_INTEGRAL_ERR_VALUE);
iVal = load_integral_value(pItem->pcfgKey);
if (iVal != DEFAULT_INTEGRAL_ERR_VALUE) {
if (pItem->value.longValue != iVal) {
@ -419,7 +413,7 @@ static void refreshCfgFileCb() {
break;
case VALUE_TYPE_FLOAT:
fVal = load_float_value(pItem->pcfgKey, DEFAULT_INTEGRAL_ERR_VALUE);
fVal = load_float_value(pItem->pcfgKey);
if (fVal != DEFAULT_INTEGRAL_ERR_VALUE) {
if (pItem->value.floatValue != fVal) {
@ -681,6 +675,21 @@ int init_config_system(const char *pCfgFile, const char *pKey) {
return ERR_SUCCESS;
}
void uninit_config_system() {
PCONFIG_ITEM pItem, pTmp;
HASH_ITER(hh, g_pConfigItem, pItem, pTmp) {
switch (pItem->valType) {
case VALUE_TYPE_ARRAY_STR:
case VALUE_TYPE_ARRAY_OBJ:
vect_destroy(pItem->value.array);
break;
default:
break;
}
}
config_destroy(&g_cfgContent);
}
const char *get_config_keygen() {
char buf[MAX_PATH];
CPU_INFO cpuInfo;
@ -703,7 +712,7 @@ const char *get_config_key(const char *pKeygen) {
unsigned char *pBase64 = (unsigned char *)base64_decode(pKeygen, (unsigned int *)&outSize);
if (pBase64 == NULL) {
printf("Base64 decode error: %s\n", pKeygen);
dzlog_error("Base64 decode error: %s\n", pKeygen);
return NULL;
}

View File

@ -96,6 +96,7 @@ long double cfg_get_float_value(CONFIG_ITEM_ID id);
int cfg_get_bool_value(CONFIG_ITEM_ID id);
long long cfg_get_integral_value(CONFIG_ITEM_ID id);
int init_config_system(const char *pCfgFile, const char *pKey);
void uninit_config_system();
void config_item_dump(const char *titleMessage);
const char *config_item_dump_fmt(const char *titleMessage);
const char *get_config_key(const char *pKeygen);

View File

@ -127,5 +127,6 @@ void user_uninit() {
free_http_server();
mq_uninit();
zlog_fini();
uninit_config_system();
uv_loop_close(get_task_manager());
}