簡體   English   中英

為什么 valgrind 在有泄漏的基本程序中檢測不到 memory 泄漏?

[英]Why won’t valgrind detect a memory leak in a basic program with a leak?

我的代碼:


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

int main() {
        char* string = malloc(50 * sizeof(char));
        printf("my code ran\n");
        return 0;

}

我編譯它:

gcc -o mem memErr.c

並運行它

valgrind ./mem

valgrind --leak-check=yes ./mem

但我的 output 是

(cmd)(base) ~/Documents/c/summerC$ valgrind --leak-check=yes ./mem
==65172== Memcheck, a memory error detector
==65172== Copyright (C) 2002-2017, and GNU GPL`d, by Julian Seward et al.
==65172== Using Valgrind-3.17.0.GIT-lbmacos and LibVEX; rerun with -h for copyright info
==65172== Command: ./mem
==65172==
==65172== Warning: set address range perms: large range [0x7fff202d0000, 0x8000200d0000) (defined)
==65172== Warning: set address range perms: large range [0x7fff202d0000, 0x7fff7ff88000) (defined)
==65172== Warning: set address range perms: large range [0x7fff8e23c000, 0x7fffc02d0000) (noaccess)
==65172== Warning: set address range perms: large range [0x7fffc02d0000, 0x7fffe2560000) (defined)
==65172== Warning: set address range perms: large range [0x7fffe2560000, 0x7fffffe00000) (noaccess)
my code ran
==65172==
==65172== HEAP SUMMARY:
==65172==     in use at exit: 0 bytes in 0 blocks
==65172==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==65172==
==65172== All heap blocks were freed -- no leaks are possible
==65172==
==65172== For lists of detected and suppressed errors, rerun with: -s
==65172== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

為什么 valgrind 不在這里報告 memory 泄漏?

編輯:根據@dbush 和@AJIOB 的評論,我嘗試將代碼更改為:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
    char* string = malloc(50 * sizeof(char));
    strcpy(string, "my new code ran");
    printf("%s\n", string);
    return 0; 
}

並編譯: gcc -g -o mem memErr.c但結果是一樣的:

(cmd)(base) ~/Documents/c/summerC$ valgrind ./mem
==81073== Memcheck, a memory error detector
==81073== Copyright (C) 2002-2017, and GNU GPL`d, by Julian Seward et al.
==81073== Using Valgrind-3.17.0.GIT-lbmacos and LibVEX; rerun with -h for copyright info
==81073== Command: ./mem
==81073==
==81073== Warning: set address range perms: large range [0x7fff202d0000, 0x8000200d0000) (defined)
==81073== Warning: set address range perms: large range [0x7fff202d0000, 0x7fff7ff88000) (defined)
==81073== Warning: set address range perms: large range [0x7fff8e23c000, 0x7fffc02d0000) (noaccess)
==81073== Warning: set address range perms: large range [0x7fffc02d0000, 0x7fffe2560000) (defined)
==81073== Warning: set address range perms: large range [0x7fffe2560000, 0x7fffffe00000) (noaccess)
my new code ran
==81073==
==81073== HEAP SUMMARY:
==81073==     in use at exit: 0 bytes in 0 blocks
==81073==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==81073==
==81073== All heap blocks were freed -- no leaks are possible
==81073==
==81073== For lists of detected and suppressed errors, rerun with: -s
==81073== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

編輯:這個問題似乎是與 valgrind 的 macOS Big Sur 兼容性問題。 我目前的解決方法是使用帶有 Docker 的虛擬 linux 環境來運行 valgrind。

由於從未使用過從 malloc 返回的malloc ,因此編譯器可能優化了調用。

valgrind output 中的這一行似乎支持了這一點:

total heap usage: 0 allocs, 0 frees, 0 bytes allocated

如果您修改代碼以便使用 malloc 的緩沖區,valgrind 應該報告泄漏:

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

int main() {
        char* string = malloc(50 * sizeof(char));
        strcpy(string, "my code ran");
        printf("%s\n", string);
        return 0;

}

來自Valgrind 快速入門指南:您應該使用-g標志(調試符號)編譯您的程序。

此外,正如@dbush 用戶所說,您應該使用您的字符串來告訴編譯器,您的變量確實是必需的。

暫無
暫無

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

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