Memory management source code

zhaozj2021-02-17  46

I saw my "Http://www.9cbs.net/develop/read_article.asp?id=11385) has 8xx points of popularity. At the request of netizens, the source code is attached to the following

These codes have implemented the following features:

1. Memory allocation, recording, and release.

2. Memory allocation location, including file name, line number record.

3. Memory leak detection and pointed out the location of the leakage, by pointing out the number of file names, line numbers, and allocation times (with VC's condition breakpoint function and then tracking it, you can find the leakage code).

4. Pointer error uses detection. Includes pointers lost, memory offline.

5. Memory usage.

I hope that everyone will support me. I am more encouraged, I will pay more about my own experience and search for good documents to make everyone progress together.

Welcome letter: lanzhengpeng@263.net

Ldebug.h

#ifndef __ldebug_h_1b29cdeb_0e25_4827_a4cc_682a48197ba6 # define __ldebug_h_1b29cdeb_0e25_4827_a4cc_682a48197ba6

#pragma overce

#ifndef assert # include #define assert assert # ENDIF

// Security Delete Pointer

#ifndef safe_delete # define safe_delete (p) {if ((p)! = null) delete (p), (p) = null;} # ENDIF

#if _debug # Pragma Warning (Disable: 4006)

#if defined (new) && defined (lnew) #undef new # Endif

Void * llib_debugnew (size_t nsize, const char * pfilename, int dwline); void llib_debugdelete (void * pmem, const char * pfilename, int dwline);

Inline void * _cdecl operator new (size_t nsize, const dwline) {return llib_debugnew (nsize, pfilename, dwline);}

Inline void * _cdecl operator new (size_t nsize) {return llib_debugnew (nsize, 0, 0);}

Inline void _cdecl operator delete (void * pmem, const char * pfilename, int dwline) {llib_debugdelete (PMEM, PFileName, dwline);}

Inline void _cdecl operator delete (void * pmem) {llib_debugdelete (PMEM, 0, 0);

#ifdef lnew #define new lnew # else #define lnew new (__ file __, __ line __) # Endif

#ENDIF / / _ Debug

#ENDIF / / __LDEBUG_H_1B29CDEB_0E25_4827_A4CC_682A48197BA6

Ldebug.cpp

#include "stdafx.h" #include "ldebug.h" #include

#if _debug

Struct Llib_Mem_Link {DWORD DWCC1; // Test Struct LLIB_MEM_LINK * PNEXT; / / The allocated memory forms a bidirectional linked list structure; dword dwlength; // allocated memory length, used for back-parallel instructions and information statistics const Char * pname; // Assign the file name DWORD dwline DWRINE DWORD DWLINE; // Assign the number of memory DWORD ID; // Distribute the number of memory DWCC2; // Effect code}; struct llib_mem_link g_llib_mem_head = {0xcdcdcd, null, null, 0, NULL, 0, 0xCDCDCDCD}; Struct Llib_Mem_Link * g_llib_mem_current = null;

Static int dwllibmemobj = 0; static int dwllibMemused = 0; static int dwllibmemmax = 0;

Static int Llib_OutputMemused () {Char BUFF [1024]; if (dwllibmemobj> 0) {llib_mem_link * p; :: sprintf (buff, "memory leak: There is% D fast memory causes% D byte memory without release / N maximum memory Use:% D byte (% D K) / N ", DWLLIBMEMOBJ, DWLIBMEMUSED, DWLLIBMMMAX, DWLLIBMEMMAX / 1024; OUTPUTDEBUGSTRING (BUFF); for (p = g_llib_mem_head.pnext; p; p = p-> pnext) {IF (P-> PNAME == NULL) {:: sprintf (buff, "unknown memory leak:% U byte (0x% 08X). -th Double Allocation! / n", P-> DWLENGTH, (Char *) (unsigned int) p sizeof (llib_mem_link)), p-> id);} else {:: sprintf (buff, "% s (% D): There is a memory leak in% U byte. Cross Sub-allocation! / N ", p-> p-> dwLength, P-> id);} outputdebugstring (buff);}} else {:: sprintf (buff," maximum memory usage:% D byte (% D k) / n ", dwllibmemmax, dwllibmemmax / 1024); outputdebugstring (buff);} return 0;}

FDIB_API void * LLib_DebugNew (size_t nSize, const char * pFileName, int dwLine) {static int dwid = 0; struct LLIB_MEM_LINK * temp; if (g_LLib_Mem_Current == NULL) {_onexit (LLib_OutputMemUsed); g_LLib_Mem_Current = & g_LLib_Mem_Head;} ​​dwLLibMemUsed = nSize ; if (dwLLibMemMax pNext = temp; temp -> dwCC1 = temp-> dwCC2 = 0xCDCDCDCD; temp-> dwLength = nSize; temp-> dwLine = dwLine; temp-> pLast = g_LLib_Mem_Current; temp-> pNext = NULL; temp-> pName = pFileName; temp-> id = dwid; g_LLib_Mem_Current = temp; DWORD * dwp = (DWORD *) (((DWORD) temp) nSize sizeof (LLIB_MEM_LINK)); * dwp = 0xCDCDCDCD; * dwp = 0xCDCDCDCD; dwLLibMemObj , dwid ; return (void *) ((DWORD) TEMP) sizeof (llib_mem_link));} else {char buff [1024]; :: sprintf (buff, "% s (% d): allocated less memory (% D bytes)! D-Distribution / N ", PfileName, DWLINE, NSIZE, DWID; OUTPUTDEBUGSTRING BUFF);} return null;}

void LLib_DebugDelete (void * pMem, const char * pFileName, int dwLine) {struct LLIB_MEM_LINK * temp = (struct LLIB_MEM_LINK *) (((DWORD) pMem) - sizeof (LLIB_MEM_LINK)); if (g_LLib_Mem_Current == temp) {g_LLib_Mem_Current = Temp-> plast;} unsigned int size = _msize (void *) Temp); if ((temp-> dwlength! = size-sizeof (llib_mem_link) - sizeof (dword) * 2) || / (Temp-> DWCC1 ! = 0xcdcdcdcd) || (TEMP-> DWCC2! = 0xcdcdcdcd)) {charff [1024]; :: sprintf ("% s (% d): pointer head corruption. Crown Double allocation! / N", TEMP -> PNAME, TEMP-> DWLINE, TEMP-> ID); OUTPUTDEBUGSTRING (BUFF);} Temp-> DWCC1 = TEMP-> DWCC2 = 0XCCCCCCCCCC; DWORD * DWP = (DWORD *) ((DWORD) PMEM) TEMP -> DWLENGTH); if (* dwp! = 0xcdcdcdcdcd || dwp [1]! = 0xcdcdcdcd) {charff [1024]; :: sprintf ("% s (% d): pointer offline. Cross-allocation! / N ", TEMP-> PNAME, TEMP-> DWLINE, TEMP-> ID); OutputDebugString (BUFF); DWP [0] = DWP [1] = 0xcccccccc; dwllibMemused - = Temp-> DWLENGTH; DWLLIBMEMOBJ -; IF (Temp-> PNext) Temp-> PNEXT-> Plast = Temp-> Plast; Temp-> Plast-> PNEXT = TEMP-> PNEXT; Free (void *) Temp);} # endif // end_debug

These codes are I cut from one of my image to handle the library, which may be compiled, please modify it. In addition, possible VC always says Operator New, Operator Delete repeats, compile this code into a DLL.

转载请注明原文地址:https://www.9cbs.com/read-29428.html

New Post(0)