- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个项目,其调用函数为
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/
我想在 GLib 中将整数值转换为字符串。是否有任何宏或函数?或者我可以在同一个双向链表中存储不同的数据类型吗? 最佳答案 gchar *my_string = g_strdup_printf("%i
所以基本上我正在编写以下步骤来创建基于 dbus 的应用程序。1. g_bus_watch_name(特定服务出现的回调)2. g_signal_connect 附加到服务提供的某些信号。 g_sig
所以基本上我正在编写以下步骤来创建基于 dbus 的应用程序。1. g_bus_watch_name(特定服务出现的回调)2. g_signal_connect 附加到服务提供的某些信号。 g_sig
我尝试使用JUCE框架构建cpp程序,但出现错误 make: *** No rule to make target '/usr/include/glib-2.0/glib/gurifuncs.h',
我有一些使用 dbus-glib 通过 dbus 编写服务器导出和接口(interface)。 我正在尝试编写一个将字节数组作为输入的方法,但是我很难确定在我的方法参数中使用什么类型。 例如,如果我有
我一直在研究 python-mpdor 的源代码它提到它是 gobject-based, for easy event handling (in the high-level client class
假设我有一个 GLib.Array并想将其转换为 Item[] ,我将如何在 Vala 中做到这一点? 最佳答案 首先,除非您需要与现有代码进行互操作,否则不要使用 GLib.Array。使用 GLi
我正在尝试使用 GLib 的 spawn_command_line_sync 将 echo 的输出通过管道传输到命令中方法。我遇到的问题是 echo 将整个命令解释为参数。 为了更好地解释,我在我的代
我似乎无法使用 glib.h 编译这个基本程序... #include glib.h #include stdio.h int main () { return ((glib_major_ver
我刚开始学习 vala。我尝试了来自 vala tutorial 的以下程序. class Demo.Hello : Glib.Object { public static int main( s
在调查一些性能问题时,我最终进入了 gthread-posix.c。 在那里我找到了如下代码: static void __attribute__((noinline)) g_mutex_lock_s
这个问题在这里已经有了答案: Undefined reference to 'pthread_create' — linker command option order (libraries bef
我正在学习使用 GLib 编写简单、高效的套接字服务器的基础知识。我正在试验 GSocketService。到目前为止,我似乎只能接受连接,但它们会立即关闭。从文档中我无法弄清楚我缺少的步骤。我希望有
我在 gobject 上阅读了一个维基百科页面,上面写着, Depending only on GLib and libc, GObject is a cornerstone of GNOME and
我尝试了sudo apt-get install libglib2.0-dev并得到了 libglib2.0-dev is already the newest version. 我仍然收到错误 co
所以我正在尝试使用 GtkSourceViewmm 在 C++ 中使用 GtkSourceView ,其文档和支持水平给我的印象是很长一段时间没有仔细研究它。但我始终是一个乐观主义者 :) 我正在尝试
我是编程新手。我正在创建一个 Qt GUI 应用程序。这是一个小的拼字游戏。问题是每当我在构建它后从命令提示符启动这个程序时,我都会在命令提示符下得到以下输出: JumbleGame 1.0 Laun
我正在尝试创建一个相当简单的使用自动工具的 BitBake 配方,您可以在这里看到: SUMMARY = "an example autotools recipe" LICENSE = "MIT" L
我正在尝试创建一个相当简单的使用自动工具的 BitBake 配方,您可以在这里看到: SUMMARY = "an example autotools recipe" LICENSE = "MIT" L
我正在查看一段代码,它直到最近才有效。基本上,我有一个 C++ 类,我在其中使用 G_LOCK_DEFINE 宏保护一个变量。 class CSomeClass { private: gulo
我是一名优秀的程序员,十分优秀!