Introduction
Have you ever had a memory leak? Wished you knew where you allocated it and how? Is your boss cheap and refuses to buy Boundchecker or another debugging tool?
Here is the solution for you. A memory leak detector compiled directly into your code. It reports memory leaks with call stack of the allocation down to a user defined depth.
As an add-on, it does simple checks of the memory before and after the memory block, to track buffer overwrites.
Usage
Include tracealloc.cpp in your project. Define DETECT_LEAKS
in the project settings. Compile. Run your application. Memory leaks are reported into your debug output window when the application terminates. Just click the memory leak and the correct file and line will be shown.
You can find further instructions in the source code.
How is it done?
The code overrides operator new
and operator delete
. For each allocation made, the code allocates extra space for tracking, call stack and no-mans-land. The current call stack is fetched and remembered, finally the code puts the newly allocated block in a linked list and the requested memory is returned.
When a memory block is deleted, the header is found and checked for buffer overwrites. The memory block is then removed from the linked list and deallocated.
When the program terminates, the global memory tracker object is deleted. The destructor traverses the linked list for memory blocks that isn’t deleted (= leaked memory). It then fetches symbol information for the call stacks and dumps the information in the debug console.
Limitations
The code is Microsoft Visual Studio and Win32 specific. It requires a debug build. The code is C++ specific. It handles new
/delete
but not malloc
/free
. The code will run slower with leak detection active (roughly at half normal debug build speed).
Finally
I want to thank Zoltan Csizmadia who wrote ExtendedTrace
. I have used parts of his code for stack walking and symbol lookups.
I also want to thank the Code Project community. I have found many solutions or pointers in the right direction here. I hope I have given something back with this contribution.
Thank you!
<!-- Article Ends --><!-- Main Page Contents End -->
License
This article, along with any associated source code and files, is licensed under The MIT License
相关推荐
memory leak 教程,一步一步解决内存泄漏。
This program is a tool that help you positioning memory leak locations of an QUALCOMM BREW application on simulator. It can give the call stack of memory leaks (including the locations that MALLOC/...
LeakFinder(觅露) 本工具仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则后果作者概不负责 LeakFinder(觅露)为s7ck Team 红队云武器库F-Box里的一款信息泄露浏览搜集浏览器插件。 信息泄露...
一个在模拟器上定位BREW应用程序内存泄露位置的工具,可以给出内存泄露位置的调用栈信息(包括MALLOC、REALLOC分配的内存,也能定位到调用ISHELL_CreateInstance的位置)
本文中描述了C++中常见的内存泄露,以及对应的解决方法,并且介绍了几种常见的内存泄露检查工具的使用(常见的工具只有vld和codesnitch)
memory leak tools for android memory leak tools for android
Memory leak detection for WinCE
检测内存泄漏的工具源码 三个文件 使用时只要include一个头文件就可以 内含一个example程序
内存泄露(memory leak)资料,网上收集的,感谢原作者
C++ memory leak detection and exception
This program is a tool that help you positioning memory leak locations of an QUALCOMM BREW application on simulator. It can give the call stack of memory leaks (including the locations that MALLOC/...
有关flex 内存泄漏memory leak介绍的ppt. flash player的内存泄漏实在搞的我头大了
Kylix Leak Finder是一个很小的非侵入性单元,您可以将其添加到项目中以查找Kylix中的内存泄漏。
使用__wrap_malloc查找malloc导致内存泄漏问题 memory leak 内存泄漏
A Practical FlowSensitive and ContextSensitive C and C++ Memory Leak Detector
This document is about Memory leak.
技術分享_Glassfish 3.1.2.2 Web Service Memory Leak Workaround1
内存泄漏检测器 MemoryLeakDetector是由Xigua视频android团队开发的本地内存泄漏监视工具。 它具有访问简单,监视范围广,性能...leak-detector:0.0.7 '} 步骤3:添加代码以方便使用(使用广播控件不需要此步骤) //
Converting a bitmap to a region - memory leak fix将位图转化为一个区域 - 修补了内存漏洞(179KB)