C mem无泄漏g_标准

C mem无泄漏g_标准,c,memory-leaks,valgrind,strdup,C,Memory Leaks,Valgrind,Strdup,我正试图释放g_strdup,但我不确定我做错了什么 使用valgrind-tool=memcheck-leak-check=yes./a.我一直得到: ==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 /l

我正试图释放g_strdup,但我不确定我做错了什么

使用valgrind-tool=memcheck-leak-check=yes./a.我一直得到:

==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)
我尝试过不同的方法来释放,但至今没有成功。我将感谢任何帮助。谢谢

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


g_strdupkey分配内存,但没有人释放内存


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

g_strdupkey分配内存,但没有人释放该内存

您可能应该为g_hash_table_new_full提供您自己的密钥_destroy_func,而不是使用g_hash_table_new。

为什么要对放入哈希表的每个密钥进行备份?你必须这样做吗?如果GTK要求您复制哈希表中的每个char*键,我敢打赌在执行g_hash_table_destroy时,它会释放THAM

检查GTK dokcumentation。

为什么要将放入哈希表的每个键都重新设置?你必须这样做吗?如果GTK要求您复制哈希表中的每个char*键,我敢打赌在执行g_hash_table_destroy时,它会释放THAM


查看GTK dokcumentation。

谢谢。就像你们提到的那样:hashtable1=g_hash_table_new_fullg_str_hash,g_str_equal,GDestroyNotify free_key_value,NULL;谢谢就像你们提到的那样:hashtable1=g_hash_table_new_fullg_str_hash,g_str_equal,GDestroyNotify free_key_value,NULL;
#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;
}