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++内存泄漏检测方法的更多相关文章

  1. VS2005内存泄漏检测方法[转载]

    一.非MFC程序可以用以下方法检测内存泄露: 1. 程序开始包含如下定义: #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __ ...

  2. C++程序内存泄漏检测方法

    一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...

  3. 【转】简单内存泄漏检测方法 解决 Detected memory leaks! 问题

    我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} n ...

  4. _CrtSetBreakAlloc简单内存泄漏检测方法,解决Detected memory leaks!问题

    我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} n ...

  5. [教程] Android Native内存泄漏检测方法

    转载请注明出处:https://www.cnblogs.com/zzcperf/p/9563389.html Android 检测 C/C++内存泄漏的方法越来越简便了,下面列举一下不同场景下检测C/ ...

  6. Windows 下的内存泄漏检测方法

    在 Windows 下,可使用 Visual C++ 的 C Runtime Library(CRT) 检测内存泄漏. 首先,我们在.c或.cpp 文件首行插入这一段代码: #define _CRTD ...

  7. 【Visual Studio】简单内存泄漏检测方法 解决 Detected memory leaks! 问题(转)

    原文转自 http://blog.csdn.net/u011430225/article/details/47840647 我的环境是: XP SP2.VS2003 最近在一个项目中, 程序退出后都出 ...

  8. C++ 简单内存泄漏检测方法

    遇到个bug,MFC程序异常退出,debug模式下输出 Detected memory leaks! Dumping objects -> {366566} normal block at 0x ...

  9. visual leak dector内存泄漏检测方法

    http://vld.codeplex.com/ QT 内存泄露时,你们一般用什么工具检测啊 ------解决方案--------------------这篇你觉得详细么 :http://newfac ...

随机推荐

  1. expect自动应答

    expect脚本 1. expect简介 expect是一个用来处理交互的命令.借助Expect,我们可以将交互过程写在一个脚本上,使之自动化完成. expect(自动应答) 基于TCL(Tool C ...

  2. 告别单调,Django后台主页改造 - 使用AdminLTE组件

    前言 之前我做了个Django的项目,为了让管理后台更加美观,我对Django(应该说是SimpleUI的)默认的Admin后台主页进行改造,具体可以看这篇文章:项目完成 - 基于Django3.x版 ...

  3. python常见的错误提示处理

    python常见的错误有 NameError变量名错误 IndentationError代码缩进错误 AttributeError对象属性错误 TypeError类型错误 IOError输入输出错误 ...

  4. 校验日期格式为yyyy-MM-dd

    /** * 校验时间 * * @param text * @return */ public static boolean checkTime(String text) { DateFormat fo ...

  5. centos7解决无法上网的问题

    问题:centos7出现无法进行联网,如下图所示,执行该命令: ping qq.com 出现如下情况: 解决方法: 首先cd到需要修改文件的所在目录下: [root@localhost ~]# cd ...

  6. 广西省行政村边界shp数据/广西省乡镇边界/广西省土地利用分类数据/广西省气象数据/降雨量分布数据/太阳辐射数据

    ​  数据下载链接:数据下载链接 广西壮族自治区,地处中国南部,北回归线横贯中部,属亚热带季风气候区.南北以贺州--东兰一线为界,此界以北属中亚热带季风气候区,以南属南亚热带季风气候区. 数据范围:全 ...

  7. 2022宁波市第五届网络安全大赛MISC方向部分wp

    BlackAndWhite 1. 得到了三百多张黑白颜色的图片,将白色图片转为数字0,黑色图片转为数字1,得到二进制字符串 01100110011011000110000101100111011110 ...

  8. 螣龙安科携手51CTO:网络安全实战课程最新发布

    一年一度的双十一狂欢节即将来临了,相信各大电商平台也正摩拳擦掌跃跃欲试中.回顾2019年,阿里巴巴双十一狂欢节的单日交易额就达到了2684亿人民币,创造了电商交易历史上新的记录. 当人们愉快地购买着自 ...

  9. MySQL经典50题

    1.查询01课程比02课程成绩高的学生的信息及课程分数 #1.1查询01课程与02课程的课程表: select student_id, score as c1_score from score whe ...

  10. SQL练习六--More JOIN operations

    movie Field name Type Notes id INTEGER An arbitrary unique identifier title CHAR(70) The name of the ...