valgrindを使用して C プログラムをチェックします。
void mprint(gpointer e, gpointer c);
int main(int argc, char **argv) {
printf("%s", "\n(paser-start \n");
GSList *list = NULL;
list = g_slist_append(list, "first");
list = g_slist_append(list, "second");
int *c = (int *) calloc(1, sizeof(int));
*c = 0;
g_slist_foreach(list, mprint, c);
free(c);
c = NULL;
g_slist_free(list);
printf("%s", "\n paser-end)\n");
return 0;
}
void mprint(gpointer e, gpointer c) {
int *counter = (int *) c;
printf("%d: %s\n", *counter, e);
(*counter)++;
}
valgrindは常に出力します: ==20375== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)
. この行にコメントすると、エラーはありません: list = g_slist_append(list, "second");
. しかし、GSList
私が見つけたすべての例はそのように示されています。それで、私はどんな間違いをしましたか?どうすればGSList
正しく作業できますか? valgrindの完全な出力:
$ ./debug.sh
rm /home/foo/bar/parser/parser
gcc -std=c99 `pkg-config --cflags --libs glib-2.0` -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -o /home/foo/bar/parser/parser /home/foo/bar/parser/parser.c
==20375== Memcheck, a memory error detector
==20375== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==20375== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==20375== Command: /home/foo/bar/parser/parser
==20375==
(paser-start
0: first
1: second
paser-end)
==20375==
==20375== HEAP SUMMARY:
==20375== in use at exit: 4,376 bytes in 12 blocks
==20375== total heap usage: 13 allocs, 1 frees, 4,380 bytes allocated
==20375==
==20375== 4 bytes in 1 blocks are still reachable in loss record 1 of 10
==20375== at 0x4C28BED: malloc (vg_replace_malloc.c:263)
==20375== by 0x4EB5085: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4EB57D8: g_private_get (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E93300: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E94672: g_slist_append (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4008DD: main (in /home/foo/bar/parser/parser)
==20375==
==20375== 40 bytes in 1 blocks are still reachable in loss record 2 of 10
==20375== at 0x4C28BED: malloc (vg_replace_malloc.c:263)
==20375== by 0x4EB511E: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4EB51DC: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4EB5418: g_mutex_lock (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E4BAC0: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E9348C: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E94672: g_slist_append (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4008DD: main (in /home/foo/bar/parser/parser)
==20375==
==20375== 40 bytes in 1 blocks are still reachable in loss record 3 of 10
==20375== at 0x4C28BED: malloc (vg_replace_malloc.c:263)
==20375== by 0x4EB511E: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4EB53E8: g_mutex_init (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E4BBFE: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E9348C: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E94672: g_slist_append (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4008DD: main (in /home/foo/bar/parser/parser)
==20375==
==20375== 40 bytes in 1 blocks are still reachable in loss record 4 of 10
==20375== at 0x4C28BED: malloc (vg_replace_malloc.c:263)
==20375== by 0x4EB511E: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4EB53E8: g_mutex_init (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E4BC28: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E9348C: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E94672: g_slist_append (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4008DD: main (in /home/foo/bar/parser/parser)
==20375==
==20375== 240 bytes in 1 blocks are possibly lost in loss record 5 of 10
==20375== at 0x4C270FE: memalign (vg_replace_malloc.c:694)
==20375== by 0x4C271A7: posix_memalign (vg_replace_malloc.c:835)
==20375== by 0x4E4BDCD: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E937E0: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E94672: g_slist_append (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4008DD: main (in /home/foo/bar/parser/parser)
==20375==
==20375== 252 bytes in 1 blocks are still reachable in loss record 6 of 10
==20375== at 0x4C272B8: calloc (vg_replace_malloc.c:566)
==20375== by 0x4E7EF88: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E4BBAB: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E9348C: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E94672: g_slist_append (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4008DD: main (in /home/foo/bar/parser/parser)
==20375==
==20375== 504 bytes in 1 blocks are still reachable in loss record 7 of 10
==20375== at 0x4C272B8: calloc (vg_replace_malloc.c:566)
==20375== by 0x4E7EF88: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E4BBCB: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E9348C: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E94672: g_slist_append (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4008DD: main (in /home/foo/bar/parser/parser)
==20375==
==20375== 504 bytes in 1 blocks are still reachable in loss record 8 of 10
==20375== at 0x4C272B8: calloc (vg_replace_malloc.c:566)
==20375== by 0x4E7EF88: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E4BBEB: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E9348C: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E94672: g_slist_append (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4008DD: main (in /home/foo/bar/parser/parser)
==20375==
==20375== 720 bytes in 3 blocks are possibly lost in loss record 9 of 10
==20375== at 0x4C270FE: memalign (vg_replace_malloc.c:694)
==20375== by 0x4C271A7: posix_memalign (vg_replace_malloc.c:835)
==20375== by 0x4E4BDCD: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E93810: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E94672: g_slist_append (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4008DD: main (in /home/foo/bar/parser/parser)
==20375==
==20375== 2,032 bytes in 1 blocks are still reachable in loss record 10 of 10
==20375== at 0x4C272B8: calloc (vg_replace_malloc.c:566)
==20375== by 0x4E7EF88: g_malloc0 (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E4BCA8: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E9348C: g_slice_alloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4E94672: g_slist_append (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.4)
==20375== by 0x4008DD: main (in /home/foo/bar/parser/parser)
==20375==
==20375== LEAK SUMMARY:
==20375== definitely lost: 0 bytes in 0 blocks
==20375== indirectly lost: 0 bytes in 0 blocks
==20375== possibly lost: 960 bytes in 4 blocks
==20375== still reachable: 3,416 bytes in 8 blocks
==20375== suppressed: 0 bytes in 0 blocks
==20375==
==20375== For counts of detected and suppressed errors, rerun with: -v
==20375== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)