答案是手写内存泄漏检测器可通过重载malloc和free记录内存分配信息。1. 重载内存分配函数,记录每次malloc/new和free/delete的调用堆栈与大小;2. 程序结束时输出未释放的内存块地址、大小及分配位置,实现简单检测。

在C++开发中,内存泄漏是常见问题。虽然有Valgrind、AddressSanitizer等强大工具,但在某些嵌入式环境或需要轻量级方案时,手写一个简单的内存泄漏检测器很有价值。下面介绍如何实现一个基础但实用的内存泄漏检测工具。
核心思想是在程序运行期间记录每一次malloc和free(或new和)操作,通过重载内存管理函数,在调用前后插入日志记录逻辑。程序退出前输出未释放的内存块信息。
主要功能包括:
在全局作用域中重载operator new和,可以捕获所有使用new和的内存操作。
立即学习“C++免费学习笔记(深入)”;
#include <cstdio>
#include <cstdlib>
#include <map>
#include <string>
<p>struct AllocInfo {
size_t size;
const char* file;
int line;
};</p><p>std::map<void*, AllocInfo> g_allocations;</p><p>void<em> operator new(size_t size, const char</em> file, int line) {
void* ptr = std::malloc(size);
if (ptr) {
g_allocations[ptr] = {size, file, line};
}
return ptr;
}</p><p>void* operator new(size_t size) {
// 如果没有使用宏包装,默认标记为 unknown
return ::operator new(size, "unknown", 0);
}</p><p>void operator delete(void* ptr) noexcept {
if (ptr == nullptr) return;
auto it = g_allocations.find(ptr);
if (it != g_allocations.end()) {
g_allocations.erase(it);
}
std::free(ptr);
}</p>直接写new(file, line)语法不合法,需要用宏替换new表达式。
#define new new(__FILE__, __LINE__)
这样所有使用new的地方都会自动带上文件名和行号。注意这个宏会影响整个翻译单元,建议只在测试构建中启用。
示例代码:
int main() {
int* p1 = new int(42); // 被捕获
int* p2 = new int[100]; // 注意:数组版本需额外重载
<pre class='brush:php;toolbar:false;'>delete[] p2; // 正常释放
// delete p1; // 故意漏掉,制造泄漏
// 程序结束前输出泄漏
for (const auto& alloc : g_allocations) {
printf("Leak: %p, size=%zu, at %s:%d\n",
alloc.first, alloc.second.size,
alloc.second.file, alloc.second.line);
}
return 0;}
运行后会输出类似:
Leak: 0x7f9a80c00000, size=4, at main.cpp:20上面例子中new int[100]不会被拦截,因为调用了。需要补充重载:
void* operator new[](size_t size, const char* file, int line) {
void* ptr = std::malloc(size);
if (ptr) {
g_allocations[ptr] = {size, file, line};
}
return ptr;
}
<p>void* operator new[](size_t size) {
return ::operator new[](size, "unknown", 0);
}</p><p>void operator delete[](void* ptr) noexcept {
::operator delete(ptr);
}</p>注意:标准允许为空实现,实际调用的是普通,所以复用已有逻辑即可。
这个检测器适用于开发调试阶段,有几点需要注意:
基本上就这些。不复杂但容易忽略细节。适合集成到小型项目或学习内存管理机制。
以上就是c++++怎么实现一个简单的内存泄漏检测器_C++中手写轻量内存泄漏检测工具实现的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号