//mytest.cpp
int main(int argc, char * argv[])
{ //line 120
const int N=10; // # of elements in array
const int g_nLargeRange = 500 * 1024 * 1024;
cout << "Start of tests" << endl;
int *p1 = new int(1); // use to cause leak
int *p2 = new int[N]; // allocate an int array
int *p3 = new int(2); // used to test wrong delete
char *cp = 0; // cp is null pointer
char ca[3]; // unintialized array
char * pLarge = NULL; // used to test set address range perms: large range
cout << "Test 1: off by one" << endl;
for (int i=1; i<N+1; i++) // one-off in loop
p2[i] = i; // err - initialize element p[N]
cout << "Test 2: access freed storage" << endl;
delete p1;
*p1 = 3; // err - accessing freed storage
cout << "Test 3: using uninitialized storage" << endl;
if (p2[0]) cout << "Junk" << endl;// err - used uninit data
cout << "Test 4: delete array using scalar delete" << endl;
delete p2; // err - delete array with scalar delete
cout << "Test 5: array delete of scalar" << endl;
delete [] p3; // err - array delete of scalar
cout << "Test 6: overlapping storage blocks" << endl;
memcpy( ca, &ca[1],2 ); // err - overlapping storage blocks
cout << "Test 7: system call using uninitialize data" << endl;
sleep( 1 & ca[0] ); // err - uninit data in system call
cout << "Test 8: set address range perms: large range" << endl;
pLarge = new char[g_nLargeRange];
cout << "Test 9: assign to null pointer - seg faults" << endl;
*cp = 'a'; // err - used null pointer (Seg fauilts)
cout << "End of tests" << endl;
return 0;
}
Using the command
valgrind --tool=memcheck --num-callers=50 --leak-check=full --log-file=memcheck ./mytest
cat memcheck
==29440== Memcheck, a memory error detector.
==29440== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==29440== Using LibVEX rev 1804, a library for dynamic binary translation.
==29440== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==29440== Using valgrind-3.3.0, a dynamic binary instrumentation framework.
==29440== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==29440== For more details, rerun with: -v
==29440==
==29440== My PID = 29440, parent PID = 21240. Prog and args are:
==29440== ./mytest
==29440==
==29440== Invalid write of size 4
==29440== at 0x804C2DB: main (mytest.cpp:133)
==29440== Address 0x40f5408 is 0 bytes after a block of size 40 alloc'd
==29440== at 0x40050B9: operator new[](unsigned) (vg_replace_malloc.c:268)
==29440== by 0x804C26D: main (mytest.cpp:126)
==29440==
==29440== Invalid write of size 4
==29440== at 0x804C31C: main (mytest.cpp:136)
==29440== Address 0x40f53a8 is 0 bytes inside a block of size 4 free'd
==29440== at 0x400576D: operator delete(void*) (vg_replace_malloc.c:342)
==29440== by 0x804C315: main (mytest.cpp:135)
==29440==
==29440== Conditional jump or move depends on uninitialised value(s)
==29440== at 0x804C34E: main (mytest.cpp:138)
==29440==
==29440== Mismatched free() / delete / delete []
==29440== at 0x400576D: operator delete(void*) (vg_replace_malloc.c:342)
==29440== by 0x804C3A6: main (mytest.cpp:140)
==29440== Address 0x40f53e0 is 0 bytes inside a block of size 40 alloc'd
==29440== at 0x40050B9: operator new[](unsigned) (vg_replace_malloc.c:268)
==29440== by 0x804C26D: main (mytest.cpp:126)
==29440==
==29440== Mismatched free() / delete / delete []
==29440== at 0x4005AFD: operator delete[](void*) (vg_replace_malloc.c:364)
==29440== by 0x804C3E0: main (mytest.cpp:142)
==29440== Address 0x40f5438 is 0 bytes inside a block of size 4 alloc'd
==29440== at 0x4004BF9: operator new(unsigned) (vg_replace_malloc.c:224)
==29440== by 0x804C27D: main (mytest.cpp:127)
==29440==
==29440== Source and destination overlap in memcpy(0xBEEEF860, 0xBEEEF861, 2)
==29440== at 0x4006949: memcpy (mc_replace_strmem.c:402)
==29440== by 0x804C41C: main (mytest.cpp:144)
==29440==
==29440== Conditional jump or move depends on uninitialised value(s)
==29440== at 0xC2A84C: sleep (in /lib/tls/libc-2.3.4.so)
==29440== by 0x804C455: main (mytest.cpp:146)
==29440==
==29440== Syscall param nanosleep(req) points to uninitialised byte(s)
==29440== at 0xC2AB60: __nanosleep_nocancel (in /lib/tls/libc-2.3.4.so)
==29440== by 0x804C455: main (mytest.cpp:146)
==29440== Address 0xbeeef684 is on thread 1's stack
==29440==
==29440== Conditional jump or move depends on uninitialised value(s)
==29440== at 0xC2A99C: sleep (in /lib/tls/libc-2.3.4.so)
==29440== by 0x804C455: main (mytest.cpp:146)
==29440== Warning: set address range perms: large range 524288000 (undefined)
==29440==
==29440== Invalid write of size 1
==29440== at 0x804C4BB: main (mytest.cpp:150)
==29440== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==29440==
==29440== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==29440== Access not within mapped region at address 0x0
==29440== at 0x804C4BB: main (mytest.cpp:150)
==29440==
==29440== ERROR SUMMARY: 10 errors from 10 contexts (suppressed: 19 from 1)
==29440== malloc/free: in use at exit: 524,288,016 bytes in 2 blocks.
==29440== malloc/free: 11 allocs, 9 frees, 524,288,591 bytes allocated.
==29440== For counts of detected errors, rerun with: -v
==29440== searching for pointers to 2 not-freed blocks.
==29440== checked 149,404 bytes.
==29440==
==29440==
==29440== 16 bytes in 1 blocks are possibly lost in loss record 1 of 2
==29440== at 0x4004BF9: operator new(unsigned) (vg_replace_malloc.c:224)
==29440== by 0x260901: std::string::_Rep::_S_create(unsigned, unsigned, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.3)
==29440== by 0x261049: std::string::_M_mutate(unsigned, unsigned, unsigned) (in /usr/lib/libstdc++.so.6.0.3)
==29440== by 0x261348: std::string::assign(char const*, unsigned) (in /usr/lib/libstdc++.so.6.0.3)
==29440== by 0x805BC8C: _GLOBAL__I__ZN3cmx29convertYearMonthDayToLongLongEiiiRy (char_traits.h:258)
==29440== by 0x805BE1C: (within /home/welkin/svn/cm30/coremail/bin/mytest/mytest)
==29440== by 0x804A8F4: (within /home/welkin/svn/cm30/coremail/bin/mytest/mytest)
==29440== by 0x805BD85: __libc_csu_init (in /home/welkin/svn/cm30/coremail/bin/mytest/mytest)
==29440== by 0xBB4D90: (below main) (in /lib/tls/libc-2.3.4.so)
==29440==
==29440== LEAK SUMMARY:
==29440== definitely lost: 0 bytes in 0 blocks.
==29440== possibly lost: 16 bytes in 1 blocks.
==29440== still reachable: 524,288,000 bytes in 1 blocks.
==29440== suppressed: 0 bytes in 0 blocks.
==29440== Reachable blocks (those to which a pointer was found) are not shown.
==29440== To see them, rerun with: --leak-check=full --show-reachable=yes
分享到:
相关推荐
Valgrind是一款开源的动态分析工具,主要用于检测C和C++程序中的内存错误,包括内存泄漏、未初始化的内存读取、无效指针访问等。它通过构建一个虚拟机,使得程序在该虚拟机上运行,从而能够对内存操作进行精细的监控...
对于 Linux 平台下的内存泄露检测,可以使用 Valgrind 工具。Valgrind 提供了类似的功能,支持多种不同的内存错误检查工具,如 Memcheck。Valgrind 的安装和使用相对简单,可以通过官网下载最新的版本并按照文档中的...
LeakTracer是一款专为C++程序设计的内存泄漏检测工具,主要应用于Linux、Solaris和HP-UX等操作系统环境。内存泄漏是编程过程中常见的问题,尤其是对于动态内存管理的语言如C++,它可能导致程序运行效率下降,甚至...
2. 动态内存检测:在程序运行时跟踪内存分配和释放情况,检查是否有未匹配的分配和释放。这类工具能够提供运行时的内存使用情况,帮助定位泄漏点。 3. 内存泄漏检测库:使用专门的库来替换标准的内存分配函数,通过...
在C/C++编程中,内存泄漏是...总的来说,正确使用内存泄漏检测工具是提高C/C++程序健壮性和稳定性的重要步骤。了解和掌握这些工具的使用方法,能帮助程序员更高效地定位和解决内存管理问题,从而编写出更高质量的代码。
`Valgrind for NDK` 是将 `valgrind` 工具移植到Android环境,特别是针对NDK编译的原生代码进行内存检测的一种实现。由于Android设备大多基于ARM架构,所以`valgrind for NDK` 特别强调支持ARM架构。 使用 `...
另一种方法是使用内存跟踪库,如Valgrind,它可以监控程序运行时的内存分配和释放情况,找出未释放的内存块。 对于跨平台的内存泄露检测,我们需要确保使用的工具或方法在Windows和Linux上都能正常工作。例如,...
Valgrind是一款强大的内存调试、性能分析和内存泄漏检测工具,尤其在Linux环境下,它被广泛用于C++程序的开发和优化。Valgrind通过构建一个虚拟机来运行你的程序,从而可以在程序运行时进行细致的监控,帮助开发者...
Linux C语言程序内存泄漏检测工具-Valgrind Valgrind 是一款 Linux 下的免费内存调试工具包,包含多个工具,如 Memcheck、Cachegrind、Helgrind、Callgrind、Massif。它可以对编译后的二进制程序进行内存使用监测...
Valgrind适用于Linux环境,可以检测多种内存错误,包括内存泄漏。LeakSanitizer是GCC和Clang编译器内置的一种内存泄漏检测工具,它可以自动在编译时插入检查代码,无需额外的源代码修改。 总的来说,防止C/C++程序...
总之,C++内存泄漏检测需要综合运用多种策略和技术,包括但不限于智能指针、自定义内存管理、使用内存检测工具等。开发者应该养成良好的编程习惯,始终关注内存的分配和释放,以编写出高效且可靠的代码。通过学习和...
18. Compuware DevPartner Java Edition:是一个 Java 内存检测工具,用于检测 Java 代码中的内存泄漏问题。 Compuware DevPartner Java Edition 工具可以检测内存泄漏问题,并提供了详细的错误信息,以便开发者...
8. **内存池技术**:对于频繁的小型内存分配,使用内存池可以提高效率并降低内存泄漏的可能性,因为所有的内存都是在一个大的连续区域分配,释放时也更容易管理。 在压缩包文件“myleaktest”中,很可能包含了演示...
在Linux平台上,内存泄漏是C++程序开发中常见的问题,可能导致系统资源耗尽,影响程序稳定性和性能。本文将深入探讨如何在Linux环境下检测和分析内存泄漏,主要针对C++程序。 首先,我们需要理解内存泄漏的基本概念...
Valgrind是一款强大的开源内存调试、内存泄漏检测和性能分析工具,主要针对C和C++程序。它通过创建一个虚拟的运行时环境,对程序进行逐指令模拟,从而能够检查程序在运行过程中出现的各种内存问题。Valgrind的内存...
Valgrind是一款强大的动态分析工具,专用于帮助开发者检测和解决Linux环境下的内存泄漏问题。下面将详细介绍Valgrind的工作原理、使用方法以及如何利用它来定位和修复内存泄漏。 Valgrind是一个开源的模拟执行引擎...
Valgrind是一款强大的开源工具,主要用于检测C++程序中的内存问题,如内存泄漏、无效内存访问和内存管理错误。它通过构建一个虚拟机来运行你的程序,从而可以在程序执行过程中进行深入的分析。Valgrind提供了多种子...