Advertisement
Guest User

MLD.cpp

a guest
May 19th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include "MLD.h"
  2.  
  3. std::vector<MemLeakInfo> MemoryInformation;
  4. std::vector<MemLeakInfo> BadCalls;
  5.  
  6. #define new MY_DEBUG_NEW
  7.  
  8. #ifdef _DEBUG
  9.  
  10. void* operator new(unsigned int Size, int Line, const char* File)
  11. {
  12.     void* ptr = (void*)malloc(Size);
  13.  
  14.     if(ptr)
  15.     {
  16.         AddMemoryInformation( (unsigned int)ptr, Line, Size, *File, MemoryInformation);
  17.     }
  18.     else
  19.     {
  20.         AddMemoryInformation( 0, Line, 0, *File, BadCalls);
  21.     };
  22.  
  23.     return ptr;
  24. };
  25.  
  26. void* operator new[](unsigned int Size, int Line, const char* File)
  27. {
  28.     void* ptr = (void*)malloc(Size);
  29.  
  30.     if(ptr)
  31.     {
  32.         AddMemoryInformation( (unsigned int)ptr, Line, Size, *File, MemoryInformation);
  33.     }
  34.     else
  35.     {
  36.         AddMemoryInformation( 0, Line, 0, *File, BadCalls);
  37.     };
  38.  
  39.     return ptr;
  40. };
  41.  
  42. void operator delete(void* ptr)
  43. {
  44.     free(ptr);
  45.     RemoveMemoryInformation( (unsigned int)ptr);
  46. };
  47.  
  48. void operator delete[](void* ptr)
  49. {
  50.         free(ptr);
  51.     RemoveMemoryInformation( (unsigned int)ptr);
  52. };
  53.  
  54. #endif
  55.  
  56. void AddMemoryInformation(unsigned int Address, unsigned int Line, unsigned int Size, unsigned char File, std::vector<MemLeakInfo> &Vect)
  57. {
  58.     MemLeakInfo NewMemLeak;
  59.  
  60.     NewMemLeak.addr = Address;
  61.     NewMemLeak.line = Line;
  62.     NewMemLeak.size = Size;
  63.     NewMemLeak.file = File;
  64.  
  65.     Vect.push_back(NewMemLeak);
  66. };
  67.  
  68. void RemoveMemoryInformation(unsigned int Address)
  69. {
  70.     std::vector<MemLeakInfo>::iterator Iter;
  71.  
  72.     for(Iter = MemoryInformation.begin(); Iter != MemoryInformation.end(); Iter++)
  73.     {
  74.         if( (*Iter).addr = Address)
  75.         {
  76.             Iter = MemoryInformation.erase( (Iter) );
  77.             break;
  78.         };
  79.     };
  80. };
  81.  
  82. void DumpInformation()
  83. {
  84.  
  85.     if( MemoryInformation.size() > 0)
  86.     {
  87.         std::vector<MemLeakInfo>::iterator MIter;
  88.         std::ofstream File("MemoryDumpInformation.txt");
  89.  
  90.         for(MIter = MemoryInformation.begin(); MIter != MemoryInformation.end(); MIter++)
  91.         {
  92.             File << "Address:" + (*MIter).addr;
  93.             File << " Size:" + (*MIter).size;
  94.             File << " File:" + (*MIter).file;
  95.             File << " Line:" + (*MIter).line;
  96.         };
  97.  
  98.         File.close();
  99.     };
  100.  
  101.     if(BadCalls.size() > 0)
  102.     {
  103.         std::vector<MemLeakInfo>::iterator BIter;
  104.         std::ofstream File("BadCalls.txt");
  105.  
  106.         for(BIter = BadCalls.begin(); BIter != BadCalls.end(); BIter++)
  107.         {
  108.             File << "File:" + (*BIter).file;
  109.             File << " Line:" + (*BIter).line;
  110.         };
  111.     };
  112.  
  113. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement