gpt4 book ai didi

c - 在 c 中使用 glib 中的 glist 时释放内存会在调用函数时出现内存读/写错误

转载 作者:行者123 更新时间:2023-11-30 19:15:44 25 4
gpt4 key购买 nike

我有一个项目,其调用函数为

void process_message(char *dev_ip, json_t *dev_config, char* message, json_t *extra_info, long col_ts, long log_counter, char *client_id, int unique_id)
{
......
char *encoded_msg;
//g_mutex_lock(&mutex_encode);
//encoded_msg = get_encoded_msg(message, charset);
//g_mutex_unlock(&mutex_encode);
encoded_msg = message;
if(encoded_msg == NULL)
{
return;
}
//note the function here////
GSList *parsed_msg = LineParser(dev_ip, encoded_msg, "\n", file_path, unique_id); //get list of parsed message
zlog_debug(c, "Number of parsed msg: %d", g_slist_length(parsed_msg));
GSList *iterator = NULL;
for (iterator = parsed_msg; iterator; iterator = iterator->next)
{
//memory read error if freed in another function///
zlog_debug(c, "Current item is '%s'\n", iterator->data);

char *encoded_single_msg = (char *)iterator->data;

}


}

我的 LineParser 函数为

GSList *LineParser(char* dev_ip, char *msg_full, char *separator, char *file_path, int unique_id)
{
GSList *parsed_msg = NULL;
char connection_id[50];
sprintf(connection_id,"%s|%d", dev_ip, unique_id);
char *msg_concat;


// inserting {file_path : last_not_complete_line} logic to TCP_CACHE
//and removing the correspoing last_line
g_mutex_lock (&mutex_hash_main);

// char *last_line = (char *) (g_hash_table_lookup((GHashTable *) g_hash_table_lookup(TCP_CACHE, connection_id), file_path));

GHashTable *t_filepath_msg_dict = NULL; //for {file_path : last_not_complete_line}
if (TCP_CACHE != NULL)
{
t_filepath_msg_dict = (GHashTable *)(g_hash_table_lookup(TCP_CACHE, connection_id));
if (t_filepath_msg_dict != NULL)
{
char *last_line = (char *) (g_hash_table_lookup(t_filepath_msg_dict, file_path));

if(last_line != NULL) //if the hash has device ip, append the value to msg
{
zlog_debug(c,"concatenating: str1: %s and str2: %s\n", last_line, msg_full);

////note the asprintf which is to be freed
asprintf(&msg_concat,"%s%s", last_line, msg_full);
g_hash_table_remove(t_filepath_msg_dict, file_path);
msg_full = msg_concat;
free(msg_concat);
}
}

}


int msg_len = strlen(msg_full);
char last_char = msg_full[msg_len - 1];
zlog_debug(c, "len of message: %d", msg_len);
zlog_debug(c, "last char is : %c", last_char);
char *token=NULL;
char *remaining_str=NULL;
token = strtok_r(msg_full, "\n", &remaining_str);

while(token != NULL)
{
if(token[0]==' ')
{
token = trimwhitespace_parser (token);
if(strcmp(token,"")==0)
{
//insert this token to GSList
parsed_msg = g_slist_prepend (parsed_msg, token);
token = strtok_r(NULL, "\n", &remaining_str);
continue;
}
}
if(strcmp(remaining_str,"")==0)
{
if(strlen(token) > 10000)
{
zlog_warn(c, "Message too big(more than 10000 len). Stop looking for new line and process msg");
g_hash_table_remove(t_filepath_msg_dict, file_path);
}
else
{
if(last_char=='\n')
{
//new line is the last character. do nothing
zlog_debug(c, "last character is new line");
}
else
{
zlog_debug(c, "last character is not new line");
//new line not received
if (t_filepath_msg_dict == NULL) //insert new record
{
GHashTable *each_filepath_msg_dict = g_hash_table_new_full(g_str_hash, g_str_equal, key_str_destroy_cb_parser, value_str_destroy_cb_parser);
zlog_debug(c,"Inserting file_path: %s to connection_id: %s", file_path, connection_id);

g_hash_table_insert(each_filepath_msg_dict, strdup(file_path), strdup(token));
g_hash_table_insert(TCP_CACHE, strdup(connection_id), each_filepath_msg_dict);
}
else //update existing record
{
zlog_debug(c,"Connection_id :%s is already found; appending/replacing file_path :%s", connection_id, file_path);
g_hash_table_insert(t_filepath_msg_dict, strdup(file_path), strdup(token));
}
g_mutex_unlock(&mutex_hash_main);
return parsed_msg;
}
}
}
//insert token to GSList
parsed_msg = g_slist_prepend (parsed_msg, token);
token = strtok_r(NULL, "\n", &remaining_str);
}
g_mutex_unlock(&mutex_hash_main);
return parsed_msg;
}

在代码中我有 asprint 作为asprintf(&msg_concat,"%s%s",last_line, msg_full);

需要释放此 msg_concat 以减少内存泄漏。但是当我最后使用 free(msg_concat) 时。我无法正确释放内存,并在 process_message 处出现 内存读取错误

错误
**for (iterator = parsed_msg; iterator; iterator = iterator->next) 
{
zlog_debug(c, "Current item is '%s'\n", iterator->data);

char *encoded_single_msg = (char *)iterator->data;

}**

如何在 LineParser 函数中正确释放 msg_concat,而不在 process_message 函数中出现内存读取错误?

返回指针的列表是否指向调用函数的 msg_concat 的相同地址,因此无法在同一个 LineParser 函数中释放?

编辑:我尝试将列表作为引用传递并将 msg_concat 返回为:

GSList *parsed_msg=NULL;
char *msg_concat = LineParser(dev_ip, encoded_msg, "\n", file_path, unique_id,&parsed_msg); //get list of parsed message

zlog_debug(c, "Number of parsed msg: %d", g_slist_length(parsed_msg));
GSList *iterator = NULL;
for (iterator = parsed_msg; iterator; iterator = iterator->next)
{
zlog_debug(c, "Current item is '%s'\n", iterator->data);

char *encoded_single_msg = (char *)iterator->data;


json_object_set_new(extra_info, "client_id", json_string(client_id));
// json_object_set_new(extra_info, "parser", json_string(parser));

#ifdef DEBUG
char *extra_info_st;
extra_info_st = json_dumps(extra_info, JSON_INDENT(4));
zlog_debug(c,"extra_info_st msg is: %s\n", extra_info_st);
int num_of_fields_after = json_object_size(extra_info);
zlog_debug(c,"extra size is : %d\n", num_of_fields_after);
free(extra_info_st);
#endif

event = create_json_object(lp_name, extra_info, encoded_single_msg, dev_ip, dev_config, mid, col_ts, col_type, log_counter);

/* send message to upper layer */
send_event_with_mid(sender, event, normalizer, repo);

// AFREE(mid);
json_decref(event);
// AFREE(encoded_single_msg);
}
//json_decref(extra_info);
free(msg_concat);
g_slist_free(parsed_msg);
}

我重新编写了 LineParser 以将 msg_concat 返回为:

char *LineParser(char* dev_ip, char *msg_full, char *separator, char *file_path, int unique_id, GSList **parsed_msg)
{
//GSList *parsed_msg = NULL;
char connection_id[50];
sprintf(connection_id,"%s|%d", dev_ip, unique_id);
char *msg_concat;


// inserting {file_path : last_not_complete_line} logic to TCP_CACHE
//and removing the correspoing last_line
g_mutex_lock (&mutex_hash_main);

// char *last_line = (char *) (g_hash_table_lookup((GHashTable *) g_hash_table_lookup(TCP_CACHE, connection_id), file_path));

GHashTable *t_filepath_msg_dict = NULL; //for {file_path : last_not_complete_line}
if (TCP_CACHE != NULL)
{
t_filepath_msg_dict = (GHashTable *)(g_hash_table_lookup(TCP_CACHE, connection_id));
if (t_filepath_msg_dict != NULL)
{
char *last_line = (char *) (g_hash_table_lookup(t_filepath_msg_dict, file_path));

if(last_line != NULL) //if the hash has device ip, append the value to msg
{
zlog_debug(c,"concatenating: str1: %s and str2: %s\n", last_line, msg_full);
asprintf(&msg_concat,"%s%s", last_line, msg_full);
g_hash_table_remove(t_filepath_msg_dict, file_path);
msg_full = msg_concat;

}
}

}


int msg_len = strlen(msg_full);
char last_char = msg_full[msg_len - 1];
zlog_debug(c, "len of message: %d", msg_len);
zlog_debug(c, "last char is : %c", last_char);
char *token=NULL;
char *remaining_str=NULL;
token = strtok_r(msg_full, "\n", &remaining_str);

while(token != NULL)
{
if(token[0]==' ')
{
token = trimwhitespace_parser (token);
if(strcmp(token,"")==0)
{
//insert this token to GSList
*parsed_msg = g_slist_prepend (*parsed_msg, token);
token = strtok_r(NULL, "\n", &remaining_str);
continue;
}
}
if(strcmp(remaining_str,"")==0)
{
if(strlen(token) > 10000)
{
zlog_warn(c, "Message too big(more than 10000 len). Stop looking for new line and process msg");
g_hash_table_remove(t_filepath_msg_dict, file_path);
}
else
{
if(last_char=='\n')
{
//new line is the last character. do nothing
zlog_debug(c, "last character is new line");
}
else
{
zlog_debug(c, "last character is not new line");
//new line not received
if (t_filepath_msg_dict == NULL) //insert new record
{
GHashTable *each_filepath_msg_dict = g_hash_table_new_full(g_str_hash, g_str_equal, key_str_destroy_cb_parser, value_str_destroy_cb_parser);
zlog_debug(c,"Inserting file_path: %s to connection_id: %s", file_path, connection_id);

g_hash_table_insert(each_filepath_msg_dict, strdup(file_path), strdup(token));
g_hash_table_insert(TCP_CACHE, strdup(connection_id), each_filepath_msg_dict);
}
else //update existing record
{
zlog_debug(c,"Connection_id :%s is already found; appending/replacing file_path :%s", connection_id, file_path);
g_hash_table_insert(t_filepath_msg_dict, strdup(file_path), strdup(token));
}
g_mutex_unlock(&mutex_hash_main);
return msg_concat;
}
}
}
//insert token to GSList
*parsed_msg = g_slist_prepend (*parsed_msg, token);
token = strtok_r(NULL, "\n", &remaining_str);
}
g_mutex_unlock(&mutex_hash_main);
return msg_concat;
}

然而,在返回和操作列表后释放 msg_concat 会导致 valgrind 的释放无效;

最佳答案

采用您的代码并进行最小的更改来处理特定的内存管理问题。您的代码中可能存在其他问题。我没有检查过。

GSList *LineParser(char* dev_ip, char *msg_full, char *separator, char *file_path, int unique_id)
{
GSList *parsed_msg = NULL;
char connection_id[50];
sprintf(connection_id,"%s|%d", dev_ip, unique_id);
char *msg_concat = NULL; //Initialize!!!


// inserting {file_path : last_not_complete_line} logic to TCP_CACHE
//and removing the correspoing last_line
g_mutex_lock (&mutex_hash_main);

// char *last_line = (char *) (g_hash_table_lookup((GHashTable *) g_hash_table_lookup(TCP_CACHE, connection_id), file_path));

GHashTable *t_filepath_msg_dict = NULL; //for {file_path : last_not_complete_line}
if (TCP_CACHE != NULL)
{
t_filepath_msg_dict = (GHashTable *)(g_hash_table_lookup(TCP_CACHE, connection_id));
if (t_filepath_msg_dict != NULL)
{
char *last_line = (char *) (g_hash_table_lookup(t_filepath_msg_dict, file_path));

if(last_line != NULL) //if the hash has device ip, append the value to msg
{
zlog_debug(c,"concatenating: str1: %s and str2: %s\n", last_line, msg_full);

////note the asprintf which is to be freed
asprintf(&msg_concat,"%s%s", last_line, msg_full);
g_hash_table_remove(t_filepath_msg_dict, file_path);
msg_full = msg_concat;
//free(msg_concat); //Not safe to free here!
}
}

}


int msg_len = strlen(msg_full);
char last_char = msg_full[msg_len - 1];
zlog_debug(c, "len of message: %d", msg_len);
zlog_debug(c, "last char is : %c", last_char);
char *token=NULL;
char *remaining_str=NULL;
token = strtok_r(msg_full, "\n", &remaining_str);

while(token != NULL)
{
if(token[0]==' ')
{
token = trimwhitespace_parser (token);
if(strcmp(token,"")==0)
{
//insert this token to GSList
parsed_msg = g_slist_prepend (parsed_msg, token);
token = strtok_r(NULL, "\n", &remaining_str);
continue;
}
}
if(strcmp(remaining_str,"")==0)
{
if(strlen(token) > 10000)
{
zlog_warn(c, "Message too big(more than 10000 len). Stop looking for new line and process msg");
g_hash_table_remove(t_filepath_msg_dict, file_path);
}
else
{
if(last_char=='\n')
{
//new line is the last character. do nothing
zlog_debug(c, "last character is new line");
}
else
{
zlog_debug(c, "last character is not new line");
//new line not received
if (t_filepath_msg_dict == NULL) //insert new record
{
GHashTable *each_filepath_msg_dict = g_hash_table_new_full(g_str_hash, g_str_equal, key_str_destroy_cb_parser, value_str_destroy_cb_parser);
zlog_debug(c,"Inserting file_path: %s to connection_id: %s", file_path, connection_id);

g_hash_table_insert(each_filepath_msg_dict, strdup(file_path), strdup(token));
g_hash_table_insert(TCP_CACHE, strdup(connection_id), each_filepath_msg_dict);
}
else //update existing record
{
zlog_debug(c,"Connection_id :%s is already found; appending/replacing file_path :%s", connection_id, file_path);
g_hash_table_insert(t_filepath_msg_dict, strdup(file_path), strdup(token));
}
g_mutex_unlock(&mutex_hash_main);
//If memory has been allocated then free it before returning.
if(msg_concat)
free(msg_concat);
return parsed_msg;
}
}
}
//insert token to GSList
parsed_msg = g_slist_prepend (parsed_msg, token);
token = strtok_r(NULL, "\n", &remaining_str);
}
g_mutex_unlock(&mutex_hash_main);
//Same here. If memory has been allocated then free it before returning.
if(msg_concat)
free(msg_concat);
return parsed_msg;
}

您可能遇到的另一个问题是“token”指向的内存是 msg_concat 指向的字符串的子集。如果您要将这些添加到列表或哈希表中,并且该列表或哈希表将在 LineParser 函数的生命周期之外持续存在,那么您应该分配新数组并将“token”复制到新的 char 数组中。然后,当列表或哈希表超出范围时,您还应该释放分配的数组。

编辑...

你有两个选择。其中之一涉及对 LineParser 函数进行重大重组,以处理已经讨论过的问题。您还必须处理一些复杂的因素。例如,我看不到的“trimwhitespace_parser”函​​数必须通过移动数组的内容来删除任何前导空格,以便第一个非空格字符位于数组的第一个位置。这是因为您稍后想要释放该内存,如果您返回原始分配内存的偏移量,那么您就会遇到麻烦。每次调用 strtok_r 并获得非 NULL 响应时,您都必须分配一个足够大的 char 数组,然后将 strtok_r 返回的字符串复制到其中。您可以使用 asprintf 函数来执行此操作,就像您在代码中所做的那样。然后,您可以使用已分配的数组而不是 token 。正如我已经提到的,您的任何处理都不应该导致您丢失/用原始指针值的偏移量覆盖原始指针值。如果发生这种情况,您以后将无法释放它。此外,您的调用函数需要承担释放返回列表中所有项目的责任。

第二个选项相当简单,但无论如何都不优雅。如果涉及修改 LineParser 函数的原型(prototype)以获取额外的 char** 参数。这个想法是,您可以将 msg_concat 分配给它,以便它“返回”到调用函数。然后调用函数承担释放它的责任。因此,您的调用函数将声明 char *ptr = NULL,然后在调用 LineParser 时将其作为 &ptr 传递。从 LineParser 返回时,并且只有在完成链表等的所有使用之后,您才可以 free(ptr) 进行清理。

关于c - 在 c 中使用 glib 中的 glist 时释放内存会在调用函数时出现内存读/写错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31947033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com