内存泄漏释放 g_strdup

发布于 2024-08-25 05:01:46 字数 2505 浏览 5 评论 0原文

我正在尝试释放 g_strdup 但我不确定我做错了什么。

使用 valgrind --tool=memcheck --leak-check=yes ./a.out 我不断得到:

==4506== 40 bytes in 10 blocks are definitely lost in loss record 2 of 9
==4506==    at 0x4024C1C: malloc (vg_replace_malloc.c:195)
==4506==    by 0x40782E3: g_malloc (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x4090CA8: g_strdup (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x8048722: add_inv (dup.c:26)
==4506==    by 0x80487E6: main (dup.c:47)

==4506== 504 bytes in 1 blocks are possibly lost in loss record 4 of 9
==4506==    at 0x4023E2E: memalign (vg_replace_malloc.c:532)
==4506==    by 0x4023E8B: posix_memalign (vg_replace_malloc.c:660)
==4506==    by 0x408D61D: ??? (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x408E5AC: g_slice_alloc (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x4061628: g_hash_table_new_full (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x40616C7: g_hash_table_new (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x8048795: main (dup.c:42)

我尝试了不同的释放方法,但到目前为止没有成功。我将不胜感激任何帮助。谢谢

顺便说一句:它编译并运行良好。


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <stdint.h>

struct s { 
    char *data;
};

static GHashTable *hashtable1;
static GHashTable *hashtable2;

static void add_inv(GHashTable *table, const char *key)
{
    gpointer old_value, old_key;
    gint value;

    if(g_hash_table_lookup_extended(table,key, &old_key, &old_value)){
        value = GPOINTER_TO_INT(old_value);
        value = value + 2;
        /*g_free (old_key);*/
    } else {
        value = 5;
    }   
    g_hash_table_replace(table, g_strdup(key), GINT_TO_POINTER(value));
}

static void print_hash_kv (gpointer key, gpointer value, gpointer user_data){
    gchar *k = (gchar *) key;
    gchar *h = (gchar *) value;
    printf("%s: %d \n",k, h); 
}

int main(int argc, char *argv[]){

    struct s t;

    t.data = "bar";

    int i,j;
    hashtable1 = g_hash_table_new(g_str_hash, g_str_equal);
    hashtable2 = g_hash_table_new(g_str_hash, g_str_equal);

    for(i=0;i<10;i++){
        add_inv(hashtable1, t.data);
        add_inv(hashtable2, t.data);
    }

    /*free(t.data);*/
    /*free(t.data);*/

    g_hash_table_foreach (hashtable1, print_hash_kv, NULL); 
    g_hash_table_foreach (hashtable2, print_hash_kv, NULL);

    g_hash_table_destroy(hashtable1);   
    g_hash_table_destroy(hashtable2);

    return 0;
}

I'm trying to free g_strdup but I'm not sure what I'm doing wrong.

Using valgrind --tool=memcheck --leak-check=yes ./a.out I keep getting:

==4506== 40 bytes in 10 blocks are definitely lost in loss record 2 of 9
==4506==    at 0x4024C1C: malloc (vg_replace_malloc.c:195)
==4506==    by 0x40782E3: g_malloc (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x4090CA8: g_strdup (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x8048722: add_inv (dup.c:26)
==4506==    by 0x80487E6: main (dup.c:47)

==4506== 504 bytes in 1 blocks are possibly lost in loss record 4 of 9
==4506==    at 0x4023E2E: memalign (vg_replace_malloc.c:532)
==4506==    by 0x4023E8B: posix_memalign (vg_replace_malloc.c:660)
==4506==    by 0x408D61D: ??? (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x408E5AC: g_slice_alloc (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x4061628: g_hash_table_new_full (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x40616C7: g_hash_table_new (in /lib/libglib-2.0.so.0.2200.3)
==4506==    by 0x8048795: main (dup.c:42)

I've tried different ways to freed but no success so far. I'll appreciate any help. Thanks

BTW: It compiles and runs fine.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <stdint.h>

struct s { 
    char *data;
};

static GHashTable *hashtable1;
static GHashTable *hashtable2;

static void add_inv(GHashTable *table, const char *key)
{
    gpointer old_value, old_key;
    gint value;

    if(g_hash_table_lookup_extended(table,key, &old_key, &old_value)){
        value = GPOINTER_TO_INT(old_value);
        value = value + 2;
        /*g_free (old_key);*/
    } else {
        value = 5;
    }   
    g_hash_table_replace(table, g_strdup(key), GINT_TO_POINTER(value));
}

static void print_hash_kv (gpointer key, gpointer value, gpointer user_data){
    gchar *k = (gchar *) key;
    gchar *h = (gchar *) value;
    printf("%s: %d \n",k, h); 
}

int main(int argc, char *argv[]){

    struct s t;

    t.data = "bar";

    int i,j;
    hashtable1 = g_hash_table_new(g_str_hash, g_str_equal);
    hashtable2 = g_hash_table_new(g_str_hash, g_str_equal);

    for(i=0;i<10;i++){
        add_inv(hashtable1, t.data);
        add_inv(hashtable2, t.data);
    }

    /*free(t.data);*/
    /*free(t.data);*/

    g_hash_table_foreach (hashtable1, print_hash_kv, NULL); 
    g_hash_table_foreach (hashtable2, print_hash_kv, NULL);

    g_hash_table_destroy(hashtable1);   
    g_hash_table_destroy(hashtable2);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2024-09-01 05:01:46

g_strdup(key) 分配内存,但没有人释放该内存。

您可能应该向 g_hash_table_new_full 提供您自己的 key_destroy_func,而不是使用 g_hash_table_new。

g_strdup(key) allocates memory, but nobody frees that memory.

You should probably provide your own key_destroy_func to g_hash_table_new_full instead of using g_hash_table_new.

橙幽之幻 2024-09-01 05:01:46

为什么要 g_strdup() 将每个键放入哈希表中?你需要这样做吗?如果 GTK 要求您复制哈希表中的每个 char* 键,我敢打赌它会在执行 g_hash_table_destroy() 时释放它们。

检查 GTK 文档。

Why to you g_strdup() every key you put into the hashtable? Are you required to do so? If GTK requires you to dup every char* key in the hashtable I'd bet it frees theam when doing g_hash_table_destroy().

Check GTK dokcumentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文