Memory Management source code

Source: Internet
Author: User

 

See my "rating C/C ++ memory management" (http://www.csdn.net/develop/read_article.asp? Id = 11385) I am very pleased with the popularity of 8xx. At the request of netizens, the source code is pasted as follows:

The Code implements the following functions:

1. Memory Allocation, recording, and release.

2. Memory Allocation location, including file name and row number record.

3. Memory leakage detection and the location of the Code that causes the leakage. by pointing out the file name, row number, and the number of allocated times (using the conditional breakpoint function of VC and then tracking it out, you can find the code that causes the leakage ).

4. Pointer Error Detection. Including pointer loss and out-of-bounds memory.

5. Memory usage.

I hope you will be more supportive of me. With more encouragement, I will share more of my own experiences and well-searched documents, so that everyone can make common progress.

Thank you for choosing lanzhengpeng@263.net.

Ldebug. h

# Ifndef _ ldebug_h_1b29cdeb_0e25_4827_a4cc_682a48197ba6
# DEFINE _ ldebug_h_1b29cdeb_0e25_4827_a4cc_682a48197ba6

# Pragma once

# Ifndef assert
# Include <assert. h>
# Define assert
# Endif

// Safely 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 char * pfilename, int 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 <malloc. h>

# If _ debug

Struct llib_mem_link
{
DWORD dwpc3; // Verification Code
Struct llib_mem_link * pnext; // create a two-way linked list for the allocated memory.
Struct llib_mem_link * plast;
DWORD dwlength; // The allocated memory length, which is used to verify memory out-of-bounds and information statistics.
Const char * pname; // name of the allocated memory file
DWORD dwline; // specifies the row number for memory allocation.
Dword id; // The number of times the memory is allocated.
DWORD dwcc2; // Verification Code
};

Struct llib_mem_link g_llib_mem_head = {0 xcdcdcd, null, null, 0, null, 0, 0, 0 xcdcdcd };
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 leakage: % d fast memory causes % d byte memory not to be released/n maximum memory usage: % d byte (% d K)/n ",
Dwllibmemobj, dwllibmemused, dwllibmemmax, dwllibmemmax/1024 );
Outputdebugstring (buff );
For (P = g_llib_mem_head.pnext; P = p-> pnext ){
If (p-> pname = NULL ){
: Sprintf (buff, "Memory leakage at unknown locations: % u bytes (0x % 08x ). Allocation at % d! /N ", p-> dwlength, (char *) (unsigned INT) P + sizeof (llib_mem_link), P-> ID );
}
Else {
: Sprintf (buff, "% s (% d): Memory leakage of % u bytes exists. Allocation at % d! /N ", p-> pname, p-> dwline, p-> dwlength, p-> ID );
}
Outputdebugstring (buff );
}
}
Else {
: Sprintf (buff, "maximum memory usage: % d bytes (% 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 <dwllibmemused) dwllibmemmax = dwllibmemused;
Temp = (llib_mem_link *) malloc (nsize + sizeof (llib_mem_link) + sizeof (DWORD) * 2 );
If (temp! = NULL)
{
G_llib_mem_current-> pnext = temp;
Temp-> dwcc2 = 0 xcdcdcd;
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 ++ = 0 xcdcdcdcd;
* DWP = 0 xcdcdcdcd;
Dwllibmemobj ++, dwid ++;
Return (void *) (DWORD) temp) + sizeof (llib_mem_link ));
}
Else
{
Char buff [1024];
: Sprintf (buff, "% s (% d): memory not allocated (% d bytes )! % D allocation/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! = 0 xcdcdcd) | (temp-> dwcc2! = 0 xcdcdcd ))
{
Char buff [1024];
: Sprintf ("% s (% d): pointer header damaged. Allocation at % d! /N ", temp-> pname, temp-> dwline, temp-> ID );
Outputdebugstring (buff );
}
Temp-> dwpc3 = temp-> dwcc2 = 0 xcccccccc;
DWORD * DWP = (DWORD *) (DWORD) pmem) + temp-> dwlength );
If (* DWP! = 0 xcdcdcd | DWP [1]! = 0 xcdcdcd)
{
Char buff [1024];
: Sprintf ("% s (% d): pointer out of bounds. Allocation at % d! /N ", temp-> pname, temp-> dwline, temp-> ID );
Outputdebugstring (buff );
}
DWP [0] = DWP [1] = 0 xcccccccc;
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 extracted from one of my image processing function libraries. They may not be compiled. Please make some modifications. In addition, VC may always say that operator new and operator Delete are repeatedly defined. Compile this code into a DLL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.