我試圖學習 CPP 指標以及釋放我使用過的所有記憶體valgrind。但不幸的是,我遇到了泄漏錯誤,我不知道我在哪里犯了錯誤。也沒有太多關于從 valgrind 中尋找錯誤的想法,因為它是一種人類可讀的方式。任何發現泄漏的指導都非常值得重視。
編譯器: g (Ubuntu 7.5.0-3ubuntu1~16.04) 7.5.0
關于片段的相關資訊
smart pointer不是故意使用的- 這是一個最小的示例代碼。因此,某些部分似乎是不必要的。
file.cpp
#include <iostream>
void algo_fun(unsigned int* ip_ptr_array_,
unsigned int ip_size_,
unsigned int** op_ptr_array_,
unsigned int* op_size_)
{
*(op_size_) = ip_size_ 2;
// following approach is good as it allocate dynamic memory
unsigned int* local = new unsigned int[*(op_size_)];
for (unsigned int i = 0; i< *(op_size_); i )
{
local[i]=i 1*3;
}
*op_ptr_array_ = &local[0];
local[3] = 87;
}
int main()
{
// input array's contetnt
unsigned int ip_size = 10;
unsigned int* ip_ptr_array = new unsigned int[ip_size];
// output data
unsigned int op_size;
unsigned int* op_ptr_array;
// filling input array
for(unsigned int i = 0; i < ip_size; i )
{
ip_ptr_array[i] = i 2*2;
}
// function calling to get output data
algo_fun(ip_ptr_array,
ip_size,
&op_ptr_array,
&op_size);
delete [] ip_ptr_array;
delete [] op_ptr_array;
return 0;
}
可在此處找到作業版本。
用于測驗的命令: valgrind --leak-check=full --show-leak-kinds=all -v ./file
Leak Summary from valgrind
==23138== LEAK SUMMARY:
==23138== definitely lost: 0 bytes in 0 blocks
==23138== indirectly lost: 0 bytes in 0 blocks
==23138== possibly lost: 0 bytes in 0 blocks
==23138== still reachable: 72,704 bytes in 1 blocks
==23138== suppressed: 0 bytes in 0 blocks
==23138==
==23138== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==23138== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
uj5u.com熱心網友回復:
tl;dr 請檢查您是否使用了最近的 Valgrind。
結合 Valgrind 和 gdb,您應該能夠看到發生了什么。
我得到以下資訊(FreeBSD 12.2、g 10.3.0、從 git HEAD 構建的 Valgrind,[作業系統和編譯器版本不相關])。我正在使用該--trace-malloc=yes選項查看所有 mallloc/free 呼叫。不要在大型??應用程式上這樣做。
$ valgrind --leak-check=full --trace-malloc=yes ./test
--61886-- malloc(72704) = 0x5800040
--61886-- calloc(1984,1) = 0x5811C80
--61886-- calloc(104,1) = 0x5812480
--61886-- calloc(224,1) = 0x5812530
--61886-- calloc(80,1) = 0x5812650
--61886-- calloc(520,1) = 0x58126E0
--61886-- calloc(88,1) = 0x5812930
--61886-- _Znam(40) = 0x58129D0
--61886-- _Znam(48) = 0x5812A40
--61886-- _ZdaPv(0x58129D0)
--61886-- _ZdaPv(0x5812A40)
--61886-- free(0x5800040)
==61886==
==61886== HEAP SUMMARY:
==61886== in use at exit: 3,000 bytes in 6 blocks
==61886== total heap usage: 9 allocs, 3 frees, 75,792 bytes allocated
_Znam 是陣列 new 的重整版本,_ZdaPv 是代碼中陣列 delete 的重整版本。對 malloc/calloc/free 的其他呼叫來自 libc/libstc 。您可能會在 Linux 或 libc 上看到不同的痕跡。
您可以--show-reachable=yes用來獲取有關可訪問記憶體的資訊。
如果我現在在 gdb 下運行。
$ gdb ./test
(gdb) b malloc
(gdb) b malloc
(gdb) r
Breakpoint 1, malloc (nbytes=96) at /usr/src/libexec/rtld-elf/rtld.c:5877
這是對鏈接加載器 ld.so 中的 malloc 的呼叫。然后我又執行了幾個 'r' 直到
Breakpoint 1, __je_malloc_initialized () at jemalloc_jemalloc.c:208
這里鏈接加載器加載了 libstdc .so,全域 malloc 被 jemalloc 取代。
獲取呼叫堆疊
(gdb) bt
#0 __je_malloc_initialized () at jemalloc_jemalloc.c:208
#1 imalloc (sopts=<optimized out>, dopts=<optimized out>) at jemalloc_jemalloc.c:1990
#2 __malloc (size=72704) at jemalloc_jemalloc.c:2042
#3 0x00000008006eb6f4 in ?? () from /usr/local/lib/gcc10/libstdc .so.6
#4 0x000000080060e2fd in objlist_call_init (list=<optimized out>, lockstate=<optimized out>) at /usr/src/libexec/rtld-elf/rtld.c:2820
#5 0x000000080060d03d in _rtld (sp=0x7fffffffe408, exit_proc=0x7fffffffe3d0, objp=0x7fffffffe3d8) at /usr/src/libexec/rtld-elf/rtld.c:811
#6 0x000000080060a8c9 in rtld_start () at /usr/src/libexec/rtld-elf/amd64/rtld_start.S:39
#7 0x0000000000000000 in ?? ()
(gdb)
請注意相同的分配大小#2。
我不打算深入研究這個記憶體的用途,它是 C 運行時的一部分。
libstdc (和 libc)故意不釋放這個記憶體(大概這要么很難,要么就是不值得)。為了過濾掉這些分配,Valgrind 使用了一個特殊的函式來請求 libstc /libc 釋放這塊記憶體。
在這種情況下的選擇是
--run-cxx-freeres=no|yes free up libstdc memory at exit on Linux
(嚴格來說,不僅僅是Linux)。
查看 Valgrind 發行說明
Release 3.12.0 (20 October 2016)
...
* New option --run-cxx-freeres=<yes|no> can be used to change whether
__gnu_cxx::__freeres() cleanup function is called or not. Default is
'yes'.
所以我假設您使用的是 3.11 或更早版本。
Finally, if I use clang /libc there is no runtime allocation (and also no freeres function).
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314984.html
上一篇:二維陣列指向什么?
下一篇:訪問超出最初宣告的指標的記憶體
