簡體   English   中英

如何修復 Gqueue memory 泄漏?

[英]How to fix Gqueue memory leaks?

我一直在測試這個小代碼,試圖學習如何使用 glib,並且在使用 Gqueue 時我想出了這個 memory 泄漏。 這是代碼

#include <stdio.h>
#include <glib.h>

int main (){
    GQueue *test = NULL;
    test = g_queue_new();
    g_queue_push_tail(test,"hola mundo");
    printf("%s \n",(char *)g_queue_peek_head(test));
    g_queue_free(test);
    return 0;
}

這是 wath valgrind 的節目

==36160== Memcheck, a memory error detector
==36160== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==36160== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==36160== Command: ./testgqueue
==36160== 
hola mundo 
==36160== 
==36160== HEAP SUMMARY:
==36160==     in use at exit: 18,636 bytes in 7 blocks
==36160==   total heap usage: 9 allocs, 2 frees, 19,684 bytes allocated
==36160== 
==36160== 24 bytes in 1 blocks are definitely lost in loss record 2 of 7
==36160==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==36160==    by 0x48B7E98: g_malloc (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.6400.6)
==36160==    by 0x48D0485: g_slice_alloc (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.6400.6)
==36160==    by 0x48D0AAD: g_slice_alloc0 (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.6400.6)
==36160==    by 0x1091E1: main (in /home/adolfo/Documents/cosas/testGlib/testgqueue)
==36160== 
==36160== LEAK SUMMARY:
==36160==    definitely lost: 24 bytes in 1 blocks
==36160==    indirectly lost: 0 bytes in 0 blocks
==36160==      possibly lost: 0 bytes in 0 blocks
==36160==    still reachable: 18,612 bytes in 6 blocks
==36160==         suppressed: 0 bytes in 0 blocks
==36160== Reachable blocks (those to which a pointer was found) are not shown.
==36160== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==36160== 
==36160== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

g_queue_clear()釋放隊列元素的 memory,但不釋放隊列本身。 你需要

g_queue_free(test);

在那之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM