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 ...
随机推荐
- [MRCTF2020]Ezpop-1|php序列化
1.打开题目获取到源代码信息,如下: Welcome to index.php <?php //flag is in flag.php //WTF IS THIS? //Learn From h ...
- NC200190 矩阵消除游戏
NC200190 矩阵消除游戏 题目 题目描述 牛妹在玩一个名为矩阵消除的游戏,矩阵的大小是 \({n}\) 行 \({m}\) 列,第 \({i}\) 行第 \({j}\) 列的单元格的权值为 \( ...
- Halcon 模板匹配实战代码(一)
模板图片:目标是获取图像左上角位置的数字 直接想法,直接用一个框将数字框出来,然后对图片进行模板匹配(不可行,因为图像中的数字不是固定的) 所以需要选择图像中的固定不变的区域来作为模板,然后根据模板区 ...
- Android 12(S) 图像显示系统 - drm_hwcomposer 简析(上)
必读: Android 12(S) 图像显示系统 - 开篇 前言 Android源码中有包含drm_hwcomposer:/external/drm_hwcomposer/ drm_hwcompose ...
- Trie 树进阶学习笔记
前言 没脑子选手发现自己什么都不会 ... \(\text{More and more vegetables, What should I do?}\) 正文 Trie 树简介 大概是人类的话都知道吧 ...
- string的底层实现
String底层实现 string在C++也是一个重要的知识,但是想要用好它,就要知道它的底层是如何写的,才能更好的用好这个string,那么这次就来实现string的底层,但是string的接口功能 ...
- osx系统使用技巧集锦
6.禁用dashboard defaults write com.apple.dashboard mcx-disabled -boolean YES && killall Dock 5 ...
- Tapdata Cloud 2.1.5来啦:新增支持Amazon RDS数据库,错误日志查询更便捷,Agent部署细节再优化
需求持续更新,优化一刻不停--Tapdata Cloud 2.1.5 来啦! 最新发布的版本中,数据连接再上新,同时新增任务报错相关信息快速查询入口,开始支持 JVM 参数自定义设置. 更 ...
- 【python】下载中国大学MOOC的视频
[python]下载中国大学MOOC的视频 脚本目标: 输入课程id和cookie下载整个课程的视频文件,方便复习时候看 网站的反爬机制分析: 分析数据包的目的:找到获取m3u8文件的路径 1. 从第 ...
- linux firewall (marker)
查看防火墙是否开启systemctl status firewalld 若没有开启则是开启状态systemctl start firewalld 关闭则start改为stop 查看所有开启的端口fi ...