方法有两种:
1. 在使用glib函数之前:设置 g_slice_set_config(G_SLICE_CONFIG_ALWAYS_MALLOC, TRUE);
例如:
#include <glib.h>
#include <stdio.h>
int count = 0;
typedef struct _Person
{
int a;
int b;
}Person;
int main(int argc, char* argv[])
{
//#ifdef DEBUG
g_slice_set_config(G_SLICE_CONFIG_ALWAYS_MALLOC, TRUE);
//#endif
Person* p1 = g_new(Person, 1);
p1->a = 10;
p1->b = 10;
Person* p2 = g_new(Person, 1);
p2->a = 20;
p2->b = 20;
GList* list = NULL;
list = g_list_append(list, p1);
list = g_list_append(list, p2);
g_printf("sizeof(int) = %d/n", sizeof(int));
list = g_list_delete_link(list,g_list_nth(list, 1));
g_free(p1);
g_free(p2);
g_list_free(list);
return 0;
}
2. 使用命令行:
G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --leak-check=full ./list_tes
就OK啦