資源簡介
功能說明:
檢查堆內存的問題,定位到文件,行數
1. 踩內存
2. 內存重復釋放
3. 內存泄露
使用方法用 dbg_malloc, dbg_free 替換原程序中的malloc, free.
適當的時候調用dbg_memory_check 以檢查內存泄露。
原理:
在申請的內存前后添加 隔離帶,并做記錄。在free, dbg_memory_check中檢查踩內存。在free中檢查重復釋放。
假設要申請的為 size ,則實際申請的為 size + gap_size (前后隔離帶的大小)
GAP_BEGIN | size | GAP_END
PS:
此文件可酌情修改,以適應不同的設備,平臺等。
多線程情況下,請對dbg_malloc_ dbg_free_ dbg_memory_check 加鎖
代碼片段和文件信息
/*
功能說明:
???檢查堆內存的問題,定位到文件,行數
???1.?踩內存
???2.?內存重復釋放
???3.?內存泄露
???
???使用方法用?dbg_malloc,?dbg_free?替換原程序中的malloc?free.
???適當的時候調用dbg_memory_check?以檢查內存泄露。
???
原理:
???在申請的內存前后添加?隔離帶,并做記錄。在free?dbg_memory_check中檢查踩內存。在free中檢查重復釋放。
???假設要申請的為?size?,則實際申請的為??size?+?gap_size?(前后隔離帶的大小)??
???GAP_BEGIN?|?size?|?GAP_END?
PS:
???此文件可酌情修改,以適應不同的設備,平臺等。
???多線程情況下,請對dbg_malloc_??dbg_free_??dbg_memory_check?加鎖???
*/
#include?“stdio.h“
#include?“stdlib.h“
#include?“string.h“
#define?MEM_NODE_MAX (100)
#define?MEM_GAP_LEN?????????????????(100)
#define?MEM_GAP_BEFORE??????????????“MEM_GAP_BEFORE__GAP01234567899876543210012345678998765432100123456789987654321001234567899876543210“?????????????????//?99
#define?MEM_GAP_END?????????????????“01234567899876543210012345678998765432100123456789987654321001234567899876543210MEM_GAP_ENDEND__GAP“ ??//?99
//?#define?MEM_GAP_LEN?????????????????(10)
//?#define?MEM_GAP_BEFORE “123456789“
//?#define?MEM_GAP_END “987654321“
#define?FILE_PATH_LEN_MAX???????????(1024)
#define?dbg_malloc(size)???dbg_malloc_(size?__FILE__?__LINE__)?
#define?dbg_free(p)????????dbg_free_(p?__FILE__?__LINE__)
#define?MEMORY_DEBUG_ERROR_NODES_FULL? (1)
#define?MEMORY_DEBUG_ERROR_NO_MEMORY? (2)
#define?MEMORY_DEBUG_ERROR_GAP_BEFORE? (3)
#define?MEMORY_DEBUG_ERROR_GAP_END? (4)
#define?MEMORY_DEBUG_ERROR_FREE? (5)
typedef?struct?memory_node_{
void*?op;
int?osize;
void*?cp;
char?filepath[FILE_PATH_LEN_MAX];
int?line;
}?memory_node;
memory_node?memory_debug_nodes[MEM_NODE_MAX]?=?{0};
void?print_block(char?s[]?char*?buf?int?len)?{
printf(“%s\n“?s);
int?i?=?0;
for?(i?=?0;?i? printf(“0x%02hhX“?*(buf?+?i));
}
printf(“\n“);
}
void?memory_error_handle(int?type){
printf(“memory_error_handle?error:%d\n“?type);
}
void?*dbg_malloc_(unsigned?int?size?char*?filepath?int?line){
void*?op?=?malloc(size?+?2?*?MEM_GAP_LEN);
if?(op?==?NULL){
printf(“MEMMORY_DEBUG_INFO:?MALLOC?FAIL\n“);
memory_error_handle(MEMORY_DEBUG_ERROR_NO_MEMORY);
return?NULL;
}
void*?cp?=?(op?+?MEM_GAP_LEN);
int?i?=?0;
int?finded?=?0;
for?(i?=?0;?i? if?(memory_debug_nodes[i].op?==?NULL){
finded?=?1;
memory_debug_nodes[i].op?=?op;
memory_debug_nodes[i].cp?=?cp;
memory_debug_nodes[i].line?=?line;
memory_debug_nodes[i].osize?=?size;
strcpy((char*)memory_debug_nodes[i].filepath?filepath);
//?fill?gaps
strcpy((char*)op?MEM_GAP_BEFORE);
strcpy((char*)(op?+?MEM_GAP_LEN?+?size)?MEM_GAP_END);
break;
}
}
if?(0?==?finded){
printf(“MEMMORY_DEBUG_INFO:?memory_debug_nodes?full?no?space?to?save\n“);
memory_error_handle(MEMORY_DEBUG_ERROR_NODES_FULL);
}
return?cp;
}
void?dbg_memory_check(){
int?i?=?0;
int?cnt_nfree?=?0;
printf(“MEMMORY_DEBUG_INFO:?MEMORY?CHECK?BEGIN=======================================\n“);
for?(i?=?0;?i?
評論
共有 條評論