C/C++内存泄漏检测方法
1. 内存泄漏
内存泄漏(Memory Leak)是指程序中已动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果。
2. 检测代码
使用链表记录每个malloc返回的指针,释放时从链表中查找并删除找到对应指针的节点。
最终输出链表,该链表记录了所有没有释放的动态内存。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h> #define output_file "leak_detector_report.txt" typedef struct {
void* addr; //the memory address
unsigned char file[128]; //the file of the memory alloc statement
unsigned int line; //the line of the memory alloc statment
unsigned int size; //the size of the memory alloced
}addr_info; typedef struct addr_info_node{
addr_info info;
struct addr_info_node *next;
}addr_info_node, *addr_info_list; pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
addr_info_list g_head = NULL; void *malloc_detector(int size, unsigned char *file, unsigned int line)
{
void *ptr = malloc(size);
if (NULL == ptr)
{
return NULL;
} addr_info_node *node = (addr_info_node*)malloc(sizeof(addr_info_node));
if (NULL == node)
{
return NULL;
}
memset(node, 0, sizeof(addr_info_node));
node->info.addr = ptr;
node->info.line = line;
node->info.size = size;
strncpy(node->info.file,file,127);
node->next = NULL;
pthread_mutex_lock(&g_mutex);
if (NULL == g_head)
{
g_head = (addr_info_node*)malloc(sizeof(addr_info_node));
if (NULL == g_head)
{
free(node);
return NULL;
}
memset(g_head, 0, sizeof(addr_info_node));
}
addr_info_node *tail = g_head;
while (NULL != tail->next)
{
tail = tail->next;
}
tail->next = node;
pthread_mutex_unlock(&g_mutex);
return ptr;
} void free_detector(void *addr)
{
//Find and Delete the info which has the address==addr from Alloc_Info_List head
/*we only detecte the memory-leak, not the wrong free-addr which not in the addr_info_list*/
pthread_mutex_lock(&g_mutex);
addr_info_node *node = g_head;
while (NULL != node->next)
{
if (node->next->info.addr == addr)
{
free(addr);
node->next = node->next->next;
break;
}
printf("2 file %s line %d,addr %p,size %d\n",
node->info.file,node->info.line,node->info.addr,node->info.size);
node = node->next;
}
pthread_mutex_unlock(&g_mutex);
} void report_info()
{
FILE *fp_write=fopen(output_file,"w+");
if(!fp_write)
{
printf("can't open file\n");
return;
}
char info[sizeof(addr_info)+128];
pthread_mutex_lock(&g_mutex);
addr_info_node *node = g_head->next;
while (NULL != node)
{
sprintf(info,"memory leak:file %s line %d,addr %p,size %d\n",
node->info.file,node->info.line,node->info.addr,node->info.size);
fwrite(info,strlen(info)+1,1,fp_write);
node = node->next;
}
fclose(fp_write);
pthread_mutex_unlock(&g_mutex);
} void thread1_malloc_free()
{
void *ptr = malloc_detector(100, __FILE__, __LINE__);
ptr = malloc_detector(200, __FILE__, __LINE__);
free_detector(ptr);
ptr = malloc_detector(200, __FILE__, __LINE__);
} void thread2_malloc_free()
{
void *ptr = malloc_detector(400, __FILE__, __LINE__);
ptr = malloc_detector(800, __FILE__, __LINE__);
free_detector(ptr);
ptr = malloc_detector(1600, __FILE__, __LINE__);
} int main()
{
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, (void *)&thread1_malloc_free, NULL);
pthread_create(&thread2, NULL, (void *)&thread2_malloc_free, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
report_info();
}
运行输出
memory leak:file memory_detector.c line 115,addr 0x7f88c0000b60,size 100
memory leak:file memory_detector.c line 123,addr 0x7f88b8000b60,size 400
memory leak:file memory_detector.c line 118,addr 0x7f88c0001120,size 200
memory leak:file memory_detector.c line 126,addr 0x7f88b8001170,size 1600
C/C++内存泄漏检测方法的更多相关文章
- VS2005内存泄漏检测方法[转载]
一.非MFC程序可以用以下方法检测内存泄露: 1. 程序开始包含如下定义: #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __ ...
- C++程序内存泄漏检测方法
一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...
- 【转】简单内存泄漏检测方法 解决 Detected memory leaks! 问题
我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} n ...
- _CrtSetBreakAlloc简单内存泄漏检测方法,解决Detected memory leaks!问题
我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} n ...
- [教程] Android Native内存泄漏检测方法
转载请注明出处:https://www.cnblogs.com/zzcperf/p/9563389.html Android 检测 C/C++内存泄漏的方法越来越简便了,下面列举一下不同场景下检测C/ ...
- Windows 下的内存泄漏检测方法
在 Windows 下,可使用 Visual C++ 的 C Runtime Library(CRT) 检测内存泄漏. 首先,我们在.c或.cpp 文件首行插入这一段代码: #define _CRTD ...
- 【Visual Studio】简单内存泄漏检测方法 解决 Detected memory leaks! 问题(转)
原文转自 http://blog.csdn.net/u011430225/article/details/47840647 我的环境是: XP SP2.VS2003 最近在一个项目中, 程序退出后都出 ...
- C++ 简单内存泄漏检测方法
遇到个bug,MFC程序异常退出,debug模式下输出 Detected memory leaks! Dumping objects -> {366566} normal block at 0x ...
- visual leak dector内存泄漏检测方法
http://vld.codeplex.com/ QT 内存泄露时,你们一般用什么工具检测啊 ------解决方案--------------------这篇你觉得详细么 :http://newfac ...
随机推荐
- jenkins部署docker
1. 先在jenkins上配置拉取代码部分,需要在git上找到项目位置,直接复制url即可 http://192.168.0.161:3000/IT-Insurance/Back.Test-Walle ...
- 内存分析器 (MAT)
内存分析器 (MAT) 1. 内存分析器 (MAT) 1.1 MAT介绍 MAT是Memory Analyzer tool的缩写.指分析工具. 1.2 MAT作用 Eclipse Memory ...
- Lepton 无损压缩原理及性能分析
作者:vivo 互联网数据库团队- Li Shihai 本文主要介绍无损压缩图片的概要流程和原理,以及Lepton无损压缩在前期调研中发现的问题和解决方案. 一.从一个游戏开始 1.1 游戏找茬 请拿 ...
- CentOS7使用LVM缩减/home空间,扩大/空间
CentOS7使用LVM缩减/home空间,扩大/空间方法:把/home里的内容备份,然后将/home文件系统所在的逻辑卷删除,扩大/文件系统.新建/home,恢复/home的原内容1.查看默认分区[ ...
- Servlet-2获取请求,响应结果
获取请求参数值1)HttpServletRequest ① 该接口是ServletRequest接口的子接口,封装了HTTP请求的相关信息,由Servlet容器创建其实现类对象并传入serv ...
- HashMap存储自定义类型键值和LinkedHashMap集合
HashMap存储自定义类型键值 1.当给HashMap中存放自定义对象时,如果自定义对象是键存在,保证键唯一,必须复写对象的hashCode和equals方法. 2.如果要保证map中存放的key和 ...
- 常用类-Java比较器
一.Comparable接口 Java中的对象,正常情况只能进行比较 == 或 !=不能使用 > 或 < 的,但是在实际开发中有需要进行对象的比较 comparable接口的使用举例: 1 ...
- HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
从单声道.立体声.环绕声发展到三维声,音频回放技术的迭代演进是为了还原真实世界的声音.其中,三维声技术使用信号处理的方法对到达两耳的声音信号进行模拟,将声场还原为三维空间,更接近真实世界.凭借这个技术 ...
- 循环结构——while、do-while、for循环
1.while循环 语法格式: while(条件判断){ 循环体 } 解释: (1)关键字while后的小括号中的内容时循环条件. (2)循环条件是一个布尔表达式,它的值为布尔类型 "真&q ...
- Chapter 02 - Let's Get Started(C#篇)
详细解释,书上有哈.直接上代码和结果. Xcode下的自定义类 (通过new file-> cocoa class创建,保持和书中名字一样RandomController),自定义的fields ...