在c中使用来自glib的glist时释放内存会在调用函数中产生内存读/写错误

时间:2015-08-11 16:21:29

标签: c memory-management glib

我的项目有一个调用函数

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无效;

1 个答案:

答案 0 :(得分:1)

获取代码并进行最少的更改以处理特定的内存管理问题。您的代码中可能还有其他问题。我没有检查过。

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函数来执行此操作。然后,您将使用已分配的数组而不是令牌。正如我已经提到的那样,你的处理过程中没有任何一个会导致你丢失/覆盖原始指针值,而原始指针值与原始指针偏移。如果发生这种情况,您将无法在以后释放它。此外,您的调用函数需要承担释放返回列表中所有项目的责任。

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