采取以下琐碎的计划:
#include <iostream>
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用valgrind运行它,我被告知有72,704 bytes in 1 blocks那些still reachable.关于是否担心仍然可以达到警告的SO已经进行了广泛的讨论 - 我并不关心这一点.我只想了解当程序本身没有分配该库中的任何对象时,如何简单地包含标准库头可能会导致仍然可以访问的警告.
这是完整的valgrind输出:
$ valgrind --leak-check=full --track-origins=yes --show-reachable=yes ./ValgrindTest
==27671== Memcheck, a memory error detector
==27671== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==27671== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==27671== Command: ./ValgrindTest
==27671==
==27671==
==27671== HEAP SUMMARY:
==27671== in use at exit: 72,704 bytes in 1 blocks
==27671== …Run Code Online (Sandbox Code Playgroud) 我正在关注'学习C艰难之路',特别是关于Valgrind的章节.本章给出了一个故意错误的程序来展示Valgrind的工作原理.
当我在Valgrind下运行练习时,我没有在我的堆栈跟踪中获得行号,只是'(在主要下面)'的错误.
我肯定用-g标志编译.
我的Valgrind输出如下:
djb@twin:~/projects/Learning/C$ valgrind ./ex4
==5190== Memcheck, a memory error detector
==5190== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==5190== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==5190== Command: ./ex4
==5190==
==5190== Use of uninitialised value of size 4
==5190== at 0x4078B2B: _itoa_word (_itoa.c:195)
==5190== by 0x407CE55: vfprintf (vfprintf.c:1619)
==5190== by 0x40831DE: printf (printf.c:35)
==5190== by 0x4052112: (below main) (libc-start.c:226)
==5190==
==5190== Conditional jump or move depends …Run Code Online (Sandbox Code Playgroud) 这是我在实际代码中遇到的问题的最小工作示例。
#include <iostream>
namespace Test1 {
static const std::string MSG1="Something really big message";
}
struct Person{
std::string name;
};
int main() {
auto p = (Person*)malloc(sizeof(Person));
p = new(p)Person();
p->name=Test1::MSG1;
std::cout << "name: "<< p->name << std::endl;
free(p);
std::cout << "done" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译它并通过Valgrind运行它时,它给了我这个错误:
肯定丢失:1 个块中 31 个字节
malloc在上面的示例中使用,因为在我的实际代码中,我在 C++ 项目中使用了 C 库,该项目malloc在内部使用了它。所以我无法摆脱malloc使用,因为我没有在代码中的任何地方明确地这样做。std::string name分配。Personc++ valgrind memory-leaks placement-new dynamic-memory-allocation
我发现我的Rails代码中存在内存泄漏 - 也就是说,我发现了代码泄漏但不泄漏的原因.我把它减少到不需要Rails的测试用例:
require 'csspool'
require 'ruby-mass'
def report
puts 'Memory ' + `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)[1].to_s + 'KB'
Mass.print
end
report
# note I do not store the return value here
CSSPool::CSS::Document.parse(File.new('/home/jason/big.css'))
ObjectSpace.garbage_collect
sleep 1
report
Run Code Online (Sandbox Code Playgroud)
据说红宝石质量可以让我看到记忆中的所有物体.CSSPool是一个基于racc的CSS解析器./home/jason/big.css是一个1.5MB的CSS文件.
这输出:
Memory 9264KB
==================================================
Objects within [] namespace
==================================================
String: 7261
RubyVM::InstructionSequence: 1151
Array: 562
Class: 313
Regexp: 181
Proc: 111
Encoding: 99
Gem::StubSpecification: 66
Gem::StubSpecification::StubLine: …Run Code Online (Sandbox Code Playgroud) 我在Linux环境中开发C++应用程序.我每天使用的工具包括带有CDT插件的Eclipse,gdb和valgrind.
其他人使用什么工具?Linux有什么可以与微软Visual Studio的光滑相媲美吗?
或者最好是所有这些而不仅仅是我的代码?我的程序使用Gtk,Loudmouth和其他一些东西,这两个(以及它们后面的一些,libgcrypto,libssl)本身导致了很多错误,我无法检测到自己的错误.是否有可能让valgrind忽略比我自己的代码更深层次的东西?
我尝试用命令安装Valgrind brew install Valgrind并且我收到一条消息"valgrind:由于上游不兼容,这个公式要么不能在比Sierra更新的macOS版本上按预期编译或运行.错误:不满意的要求使这个版本失败."
我尝试使用该命令安装brew intall --HEAD Valgrind,在成功安装依赖项autoconf,automake和libtool后,当它尝试安装valgrind时,我收到配置错误:
"Valgrind适用于Darwin 10.x,11.x,12.x, 13.x,14.x,15.x,16.x和17.x(Mac OS X 10.6/7/8/9/10/11和macOS 10.12/13)"
我的操作系统是macOs Mojave(10.14),那么这是否意味着我无法安装一个功能正常的Valgrind和Homebrew?
我试图在Unix中使用addr2line命令,但每次它提供与??:0相同的输出.我正在命令addr2line -e a.out 0x4005BDC.我在使用valgrind工具运行这个a.out可执行文件时得到了这个地址,以找到内存泄漏.我还用-g选项编译了源代码.
我刚刚在C++中编写了一个代码进行字符串操作的代码,但是当我运行valgrind时,它显示了一些可能的内存泄漏.将代码调试到粒度级别我编写了一个简单的C++程序,如下所示:
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
std::string myname("Is there any leaks");
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
并运行valgrind我得到:
==20943== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 26 from 1)
==20943== malloc/free: in use at exit: 360,645 bytes in 12,854 blocks.
==20943== malloc/free: 65,451 allocs, 52,597 frees, 2,186,968 bytes allocated.
==20943== For counts of detected errors, rerun with: -v
==20943== searching for pointers to 12,854 not-freed blocks.
==20943== checked 424,628 bytes.
==20943==
==20943== LEAK SUMMARY:
==20943== definitely lost: 0 bytes in …Run Code Online (Sandbox Code Playgroud) 我正在尝试memcheck我写的C python扩展,但是我在设置valgrind以使用python时遇到了麻烦.我真的很感激一些建议.仅供上下文使用,这是Ubuntu 13.10,python 2.7.5+和valgrind 3.8.1.
根据Readme.valgrind我的建议,我做了以下.
1)用.下载python源码
sudo apt-get build-dep python2.7
apt-get source python2.7
Run Code Online (Sandbox Code Playgroud)
2)应用代码补丁,即"在Objects/obmalloc.c中取消注释Py_USING_MEMORY_DEBUGGER".
3)应用抑制补丁,即"取消注释Misc/valgrind-python.supp中的行,以抑制PyObject_Free和PyObject_Realloc的警告"
4)编译python与
./configure --prefix=/home/dejan/workspace/python --without-pymalloc
make -j4 install
Run Code Online (Sandbox Code Playgroud)
请注意,我做了2和3,而README.valgrind说做2或3 ...更多不能伤害.
现在,让我们在一些示例python代码中对此进行测试 test.py
print "Test"
Run Code Online (Sandbox Code Playgroud)
让我们用这个脚本在python上运行valgrind
valgrind --tool=memcheck --leak-check=full --suppressions=python2.7-2.7.5/Misc/valgrind-python.supp bin/python test.py
Run Code Online (Sandbox Code Playgroud)
出乎意料的是,仍然有来自valgrind的大量报告,其中第一个报告(以及更多关注报告)
==27944== HEAP SUMMARY:
==27944== in use at exit: 857,932 bytes in 5,144 blocks
==27944== total heap usage: 22,766 allocs, 17,622 frees, 4,276,934 bytes allocated
==27944==
==27944== 38 bytes in 1 blocks are possibly lost in loss record 24 of 1,343
==27944== …Run Code Online (Sandbox Code Playgroud)