簡體   English   中英

為什么 valgrind 檢測不到 memory 泄漏?

[英]Why doesn't valgrind detect memory leaks?

我試圖弄清楚為什么當我故意避免釋放 memory 時valgrind沒有檢測到任何錯誤。 我有這個小程序。 它從鍵盤讀取數字並打印消息You've entered <number>! . 如果之前已閱讀過該號碼,則打印You've already read <number>! .

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

struct node {
    int number;
    struct node* next;
};

struct node* add(struct node* head, int number) {
    struct node* n;
    struct node* p;
    
    n = (struct node*)malloc(sizeof(struct node));
    n->number = number;
    n->next = NULL;

    if (head == NULL) {
        return n;
    }

    p = head;
    while (p->next != NULL) {
        p = p->next;
    }
    p->next = n;
    return head;
}

void clear(struct node* head) {
    if (head == NULL) {
        return;
    }
    clear(head->next);
    free(head);
}

int already_read(struct node* head, int number) {
    struct node* p;
    if (head == NULL) {
        return 0;
    }

    p = head;
    while (p!=NULL && p->number!=number) {
        p = p->next;
    }
    
    if (p == NULL) {
        return 0;
    }
    return 1;
}

int main(int argc, char** argv) {
    int number;
    struct node* head = NULL;
    while (scanf("%d", &number) == 1) {
        if (already_read(head, number)) {
            printf("You've already entered %d!\n", number);
        }
        else {
            head = add(head, number);
            printf("You entered %d!\n", number);
        }
    }
    clear(head);
    return 0;
}

I compiled it using gcc (This file is called program.c on my system, so I used the command gcc -Wall -g -o program program.c ). 它編譯得很好。 然后,當我使用 valgrind ( valgrind./program ) 運行它時,我輸入一些數字以查看程序是否正常工作,然后使用CTRL-C停止它。 我沒有錯誤。 偉大的。

LEAK SUMMARY:
    definetely lost: 0 bytes in 0 blocks
    indirectly lost: 0 bytes in 0 blocks
      possibly lost: 0 bytes in 0 blocks
    still reachable: 64 bytes in 4 blocks
         suppressed: 0 bytes in 0 blocks
ERROR SUMMARY: 0 errors from 0 context (suppressed: 8 from 6)

然后我 go 回到代碼中。 我將所有內容保持原樣,除了我將main()中讀取clear(head)的行更改為//clear(head) 所以現在程序不再釋放空間了。 它有泄漏。 我用valgrind重新編譯並再次運行它。 我輸入與以前相同的輸入。 而且...我得到相同的錯誤報告。 和上面那個完全一樣。 我不明白為什么會這樣。 它不應該報告 memory 泄漏,因為我沒有釋放 memory 嗎? 難道我做錯了什么?

如果我在 clear call 注釋掉的情況下運行代碼並輸入 1, 2, 3, 2, q 那么我得到

==60380== 48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==60380==    at 0x4839755: malloc (vg_replace_malloc.c:307)
==60380==    by 0x401178: add (test.c:14)
==60380==    by 0x401335: main (test.c:63)
==60380== 
==60380== LEAK SUMMARY:
==60380==    definitely lost: 16 bytes in 1 blocks
==60380==    indirectly lost: 32 bytes in 2 blocks
==60380==      possibly lost: 0 bytes in 0 blocks
==60380==    still reachable: 0 bytes in 0 blocks
==60380==         suppressed: 0 bytes in 0 blocks

命令valgrind --leak-check=full./test

這正是我所期望的。 這是在 amd64 上,所以每個節點都是 16 個字節。 分配了 3 個節點(用於 1、2 和 3,但不用於重復的 2 或 q)。 main()結束時, head變量從 scope 中消失。 所以在終止時不存在指向頭(1)節點的指針->這是一個明確的泄漏。 由於鏈表,確實存在指向 (2) 和 (3) 節點的指針(從頭節點到 (2) 和從 (2) 到 (3) 的指針。因此 (2) 和 (3) 節點是間接泄漏。

暫無
暫無

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

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