"C + +" small Project--memory leak detector

Source: Internet
Author: User

In C + +, pointers tend to forget to release. Cause a memory leak.

1. Memory leaks refer to:

Memory leaks, also known as "storage Leaks", are dynamically opened up with dynamic storage allocation functions and are not released after they have been used, resulting in the deposit element being occupied. Until the end of the program. (In fact, the memory space is not recycled after use) is called a memory leak.

Memory leaks The image metaphor is that "the storage space that the operating system can provide to all processes is being squeezed by a process", the end result is that the longer the program runs, the more storage space is consumed, and eventually the entire storage space is exhausted and the whole system crashes. So a "memory leak" is from the operating system's point of view. The storage space here does not refer to the physical memory, but the virtual memory size, which depends on the size of the disk swap area setting. A piece of memory requested by the program, if there is no one pointer to it, then this memory is leaking.

And then today we made a memory leak detection tool, which is exactly the definition of an interface through which the solution to the class deposit leak can be implemented.

2. Design ideas:

The pointers we open are stored in a linked list of pointers related information through our own defined interfaces. Defines a linked list as a global variable. The global variables are then used to determine the detail tables of all our dynamic spaces, or we can use a singleton pattern to solve them. The approximate plot is: 650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/78/7F/wKiom1Z-Sj_h_4jZAAA1t9XeL1w714.png "title = "z_ze4z}pmdi$ ' p5w3ld0[ra.png" alt= "Wkiom1z-sj_h_4jzaaa1t9xel1w714.png"/> Then we first define the node type:

structmemlistnode{void* _ptr;string _file;int _line;//only with writing constructor Memlistnode (void* ptr = 0,const char* file = "", int line = 0 ): _ptr (PTR), _file (file), _line (line) {}};

Then we define global variables, use macro commands, templates, and type extraction to accomplish the functionality of our memory leak detection:

Memorycheck.hpp

#include  <iostream> #include <assert.h> #include <list> #include <string> #include " TYPETRAITS.HPP "using namespace std; #define &NBSP;NEW (type) __new<type> (sizeof (type), __file__,__ line__) #define &NBSP;DELETE (type,ptr) __delete<type> (PTR) #define  new_array (Type,num) __newarray< Type> (sizeof (type) *num+4,__file__,__line__,num) #define  delete_array (TYPE,PTR) __deletearray<type> (PTR) structmemlistnode{void* _ptr;string _file;int _line;//only with the writing constructor Memlistnode (void* ptr  = 0,const char* file =  "", int line = 0): _ptr (PTR), _file (file), _ Line {}};list<memlistnode> memlist;void* alloc (size_t size,const char*  File,int line) {void *ptr = malloc (size), if (PTR) {memlistnode info (ptr,file,line); Memlist.push_back (info);} Return ptr;} Void dealloc (void* ptr) {void* del = ptr;if (PTR) {list<memlistnode>::Iterator it = memlist.begin (); while (Memlist.end ()  != it) {if (it->_ptr ==  PTR) {memlist.erase (it); free (del); return;} ++it;} printf ("The free pointer has no space to apply \ n");}} Template<class t>t* __new (Size_t size,const char* file,int line) {T*  ptr =  (t*) Alloc (Size,file,line), if (Typetraits<t>::__ispodtype (). Get ()) {return ptr;} Else{return new (PTR)  t;}} Template<class t>void __delete (t* ptr) {if (Typetraits<t>::__ispodtype (). Get ()) {Dealloc (PTR);} Else{ptr->~t ();D ealloc (PTR);}} Template<class t>t* __newarray (size_t size,const char* file,int line,size_ T num) {t* ptr =  (t*) Alloc (Size,file,line), if (Typetraits<t>::__ispodtype (). Get ()) {return ptr;} else{* (int *) ptr = num; t* cur =  (t*) ((int) ptr+4); for (Int i =0;i < num;++i) {new (cur) t;cur =   (t*) ((int) cur + sizEOF (T));} Return ptr;}} Template<class t>void __deletearray (t* ptr) {if (Typetraits<t>::__ispodtype (). Get ()) {Dealloc (PTR);} else{int num = * ((int *) PTR); t* cur =  (t*) ((int) ptr+4); for (int i = 0;i < num;++i) {Cur->~T (); cur =  (t*) ((int) cur + sizeof (T));} Dealloc (PTR);}} Void print () {list<memlistnode>::iterator it = memlist.begin (); if (MemList.end ()   == it) {cout<< "Pointer has been all released" &LT;&LT;ENDL;} while (Memlist.end ()  != it) {int i = 1;printf ("%d: pointer address%p, file name:%s, line number:%d\n", i++,it->_ Ptr,it->_file.c_str (), it->_line); ++it;}}


Type extraction We write a separate file:

#pragma  once#include<iostream>using namespace std;struct __truetype{bool get () { return true;}}; Struct __falsetype{bool get () {return false;}}; template <class _tp>struct typetraits{   typedef __falsetype   __IsPODType;}; template <>struct typetraits< bool>{   typedef __truetype      __IsPODType;}; template <>struct typetraits< char>{   typedef __truetype      __IsPODType;}; Template <>struct typetraits< unsigned char >{   typedef  __TrueType     __IsPODType;}; Template <>struct typetraits< short>{   typedef __truetype      __IsPODType;}; Template <>struct typetraits< unsigned short >{   typedef __truetype     __ispodtype;}; template <>struct typetraits< int>{   typedef __truetype      __IsPODType;}; Template <>struct typetraits< unsigned int >{   typedef  __TrueType     __IsPODType;}; template <>struct typetraits< long>{   typedef __truetype      __IsPODType;}; Template <>struct typetraits< unsigned long >{   typedef  __TrueType     __IsPODType;}; template <>struct typetraits< long long >{   typedef  __truetype     __ispodtype;}; Template <>struct typetraits< unsigned long long>{   typedef __truetype     __ispodtype;}; Template <>struct typetraits< float>{   typedef __truetype      __IsPODType;}; Template <>struct typetraits< double>{   typedef __truetype      __IsPODType;}; Template <>struct typetraits< long double >{   typedef  __TrueType     __IsPODType;}; template <class _tp>struct typetraits< _tp*>{   typedef __ truetype     __ispodtype;};

A simple test case:

#include "memorychilke.hpp" #include "typetraits.hpp" void Test () {string* P1 = NEW (string);D elete (STRING,P1); int *p2 = NEW (int); string* p3 = New_array (string,10);D Elete_array (STRING,P3);p rint ();} int main () {Test (); return 0;}

It's done. As long as we have the basic knowledge, seriously to see what will be done.

This article from "Left Egg June" blog, reproduced please contact the author!

"C + +" small Project--memory leak detector

Related Article

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.