我正在尝试使用调试符号编译程序,以便valgrind将为我提供行号.我发现如果我一次编译一个简单的测试程序(使用-g),那么它包含符号.但是,如果我在两次传递中编译(即编译然后链接),那么它不包含调试符号.
这是单通案例的编译命令:
g++ -g file.c -o file
Run Code Online (Sandbox Code Playgroud)
两次通过
g++ -g -c file.c -o file.o
g++ -g file.o -o file
Run Code Online (Sandbox Code Playgroud)
实际程序看起来像这样,包含一个简单的无效写入
int main(){
int* x = new int[10];
x[10]=1;
Run Code Online (Sandbox Code Playgroud)
}
如果我用一次传递编译,那么valgrind给出以下内容(注意结尾处的行号)
==24114== 40 bytes in 1 blocks are definitely lost in loss record 2 of 9
==24114== at 0xB823: malloc (vg_replace_malloc.c:266)
==24114== by 0x5768D: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==24114== by 0x576DA: operator new[](unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==24114== by 0x100000F09: main (file.c:3)
Run Code Online (Sandbox Code Playgroud)
而如果我在两遍中编译,我得到这个(没有行号):
==24135== 40 bytes in 1 blocks are definitely …Run Code Online (Sandbox Code Playgroud) 说我有以下程序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int * i;
if ((i = malloc(sizeof(int) * 100)) == NULL) {
printf("EROOR: unable to allocate memory \n");
return -1;
}
/* memory is allocated successfully */
/* memory is not free'ed but program terminates */
// free(i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的程序调用malloc分配一些内存而不调用free取消分配它.程序终止而不取消分配内存.
Valgrind清楚地发现内存泄漏.
<snap>
==14209== HEAP SUMMARY:
==14209== in use at exit: 400 bytes in 1 blocks
==14209== total heap usage: 1 allocs, 0 frees, 400 bytes allocated …Run Code Online (Sandbox Code Playgroud) 出于好奇,我编写了几个不同版本的矩阵乘法,并对其运行cachegrind.在下面的结果中,我想知道哪些部分是L1,L2,L3未命中和引用以及它们的真正含义是什么?下面是我的矩阵乘法代码,万一有人需要.
#define SLOWEST
==6933== Cachegrind, a cache and branch-prediction profiler
==6933== Copyright (C) 2002-2012, and GNU GPL'd, by Nicholas Nethercote et al.
==6933== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==6933== Command: ./a.out 500
==6933==
--6933-- warning: L3 cache found, using its data for the LL simulation.
--6933-- warning: pretending that LL cache has associativity 24 instead of actual 16
Multiplied matrix A and B in 60.7487 seconds.
==6933==
==6933== I refs: 6,039,791,314
==6933== I1 misses: 1,611 …Run Code Online (Sandbox Code Playgroud) 我正在自学C.我的目标是创建一个C函数,它只是遍历一个查询字符串并在&符号和等号上分开.我对Valgrind的这个错误感到困惑.
==5411== Invalid free() / delete / delete[] / realloc()
==5411== at 0x402AC38: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5411== by 0x804857C: main (leak.c:28)
==5411== Address 0x420a02a is 2 bytes inside a block of size 8 free'd
==5411== at 0x402AC38: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5411== by 0x804857C: main (leak.c:28)
==5411==
==5411==
==5411== HEAP SUMMARY:
==5411== in use at exit: 0 bytes in 0 blocks
==5411== total heap usage: 1 allocs, 2 frees, 8 bytes allocated
==5411==
==5411== All heap blocks were freed -- no …Run Code Online (Sandbox Code Playgroud) 我有一个使用GTK +和Glade的应用程序; 使用memcheck工具运行Valgrind会输出大约2,000个错误.有没有人有一个很好的抑制文件,他们可以分享GTK应用程序?
我尝试了这些,但每个仍然留下了约900个错误:
我可以在这里找到我正在使用的代码的副本.做make run构建GUI(这个代码副本中只有GUI).
我试图通过去除我不关心的噪音和计算来描述(使用Callgrind)我的代码的特定部分.这是我想要做的一个例子:
for (int i=0; i<maxSample; ++i) {
//Prepare data to be processed...
//Method to be profiled with these data
//Post operation on the data
}
Run Code Online (Sandbox Code Playgroud)
我的用例是一个回归测试,我想确保所讨论的方法仍然足够快(类似于自上次实现以来少于10%的额外指令).这就是为什么我想从Callgrind获得更清晰的输出.(我需要一个for循环才能处理大量数据,以便对我想要分析的方法的行为有一个很好的估计)
我的第一次尝试是将代码更改为:
for (int i=0; i<maxSample; ++i) {
//Prepare data to be processed...
CALLGRIND_START_INSTRUMENTATION;
//Method to be profiled with these data
CALLGRIND_STOP_INSTRUMENTATION;
//Post operation on the data
}
CALLGRIND_DUMP_STATS;
Run Code Online (Sandbox Code Playgroud)
添加Callgrind宏来控制检测.我还添加了--instr-atstart = no选项,以确保我只分析我想要的部分代码...
不幸的是,当我开始使用callgrind启动我的可执行文件时,这种配置永远不会结束......这不是一个缓慢的问题,因为完整的仪器运行持续不到一分钟.
我也试过了
for (int i=0; i<maxSample; ++i) {
//Prepare data to be processed...
CALLGRIND_TOGGLE_COLLECT;
//Method to be profiled with these data
CALLGRIND_TOGGLE_COLLECT;
//Post …Run Code Online (Sandbox Code Playgroud) 我无法创建一个Qt GUI应用程序,在valgrind中没有超过1K'肯定丢失'字节.我已经尝试过这个,制作只展示一个QWidget的最小应用程序,扩展QMainWindow; 只是创建一个QApplication对象而不显示它或不执行它或两者都没有,但它们总是泄漏.
试图解决这个问题我已经读过,因为X11或glibc有错误,或者因为valgrind给出误报.在一个论坛帖子中,似乎暗示在main函数中创建一个QApplication-object并返回对象的exec()函数,就像在教程中所做的那样,是一种制作GUI的"简化"方法(并不一定好) , 也许?).
valgrind输出确实提到了libX11和libglibc,还提到了libfontconfig.其余的记忆损失,5个损失记录,发生在??? in libQtCore.so期间QLibrary::setFileNameAndVersion.
如果有一种更合适的方式来创建GUI应用程序,甚至可以防止其中的一部分发生,那么它是什么?如果任何valgrind输出只是噪声,我如何创建一个抑制正确的抑制文件?
编辑:谢谢你的意见和解答!
我并不担心丢失的几个KB本身,但如果我不必过滤几个错误的屏幕但通常可以从valgrind获得"OK",那么找到我自己的内存泄漏会更容易.如果我要压制警告,我最好知道它们是什么,对吧?
有趣的是看到接受的泄漏是多少!
安装:
bzip2 -d valgrind-3.10.1.tar.bz2
tar -xf valgrind-3.10.1.tar
Run Code Online (Sandbox Code Playgroud)
然后:
./configure
make
make install
Run Code Online (Sandbox Code Playgroud)
或者更简单
sudo apt-get install valgrind
Run Code Online (Sandbox Code Playgroud)
如何在这个简单的程序example1.c上运行valgrind
#include <stdlib.h>
int main()
{
char *x = malloc(100); /* or, in C++, "char *x = new char[100] */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
跑:
valgrind --tool=memcheck --leak-check=yes example1
valgrind: example1: command not found
Run Code Online (Sandbox Code Playgroud)
控制台输出:
valgrind: example1: command not found
Run Code Online (Sandbox Code Playgroud) 我在通过 Homebrew 在 macOS Big Sur 上安装 Valgrind 时遇到问题。在终端我尝试过
$ brew install valgrind
Run Code Online (Sandbox Code Playgroud)
Homebrew 自动更新后,我收到以下消息:
valgrind: Linux is required for this software.
Error: An unsatisfied requirement failed this build.
Run Code Online (Sandbox Code Playgroud)
有什么办法可以安装 Valgrind 吗?
在我之前的帖子中,我需要在10台计算机之间分发pgm文件的数据.在Jonathan Dursi和Shawn Chin的帮助下,我整合了代码.我可以编译我的程序,但它有分段错误.我跑了但什么都没发生
mpirun -np 10 ./exmpi_2 balloons.pgm output.pgm
结果是
[ubuntu:04803] *** Process received signal ***
[ubuntu:04803] Signal: Segmentation fault (11)
[ubuntu:04803] Signal code: Address not mapped (1)
[ubuntu:04803] Failing at address: 0x7548d0c
[ubuntu:04803] [ 0] [0x86b410]
[ubuntu:04803] [ 1] /lib/tls/i686/cmov/libc.so.6(fclose+0x1a0) [0x186b00]
[ubuntu:04803] [ 2] ./exmpi_2(main+0x78e) [0x80492c2]
[ubuntu:04803] [ 3] /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6) [0x141bd6]
[ubuntu:04803] [ 4] ./exmpi_2() [0x8048aa1]
[ubuntu:04803] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 4803 on node ubuntu exited on signal …Run Code Online (Sandbox Code Playgroud)