Bootstrap

C++实现内存泄漏检查

一、实现原理

对应用程序堆内存分配器(C:malloc、calloc、realloc、free,C++:new、delete、new[]、delete[])进行接管,用链表记录管理堆内存分配和释放,在程序退出时将未释放的内存信息输出至文本。

#pragma once

#include <new>

void* mymalloc(size_t, const char*, size_t);
void* mycalloc(size_t, size_t, const char*, size_t);
void* myrealloc(void*, size_t, const char*, size_t);
void myfree(void *);

#if defined _DEBUG && defined _DETECT_MEMORY_LEAK
#define  malloc(size)			mymalloc (size, __FILE__, __LINE__)
#define  calloc(num, size)		mycalloc (num, size, __FILE__, __LINE__)
#define  realloc(address, size)	myrealloc(address, size, __FILE__, __LINE__)
#define  free(mem_ref)			myfree(mem_ref)

void* __CRTDECL operator new(size_t, const char*, size_t);
void* __CRTDECL operator new[](size_t, const char*, size_t);

void* __CRTDECL operator new(size_t);
void* __CRTDECL operator new(size_t, std::nothrow_t const&) throw();
void* __CRTDECL operator new[](size_t);
void* __CRTDECL operator new[](size_t, std::nothrow_t const&) throw();

void __CRTDECL operator delete(void*, const char*, size_t);
void __CRTDECL operator delete[](void*, const char*, size_t);

void __CRTDECL operator delete(void*) throw();
void __CRTDECL operator delete(void*, std::nothrow_t const&) throw();
void __CRTDECL operator delete[](void*) throw();
void __CRTDECL operator delete[](void*, std::nothrow_t const&) throw();

//void __CRTDECL operator delete(void *, size_t) throw();
//void __CRTDECL operator delete[](void *, size_t) throw();

#define  new					new(__FILE__, __LINE__)
#endif

C++实现内存泄漏检查实现源码下载
https://download.csdn.net/download/qq_43148810/85101893

其他相关资源推荐:
侯捷C++内存管理课件和代码下载
内存池源码下载

有错误或不足欢迎评论指出!创作不易,转载请注明出处。如有帮助,记得点赞关注哦(⊙o⊙)
更多内容请关注个人博客:https://blog.csdn.net/qq_43148810

;