ZenLib
MemoryDebug.h
Go to the documentation of this file.
1 /* Copyright (c) MediaArea.net SARL. All Rights Reserved.
2  *
3  * Use of this source code is governed by a zlib-style license that can
4  * be found in the License.txt file in the root of the source tree.
5  */
6 
7 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 //
9 // MemoryDebug
10 //
11 // Provide "new" and "delete" overloadings to be able to detect memory leaks
12 // Based on http://loulou.developpez.com/tutoriels/moteur3d/partie1/ 2.2.1
13 //
14 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15 
16 //---------------------------------------------------------------------------
17 #ifndef ZenMemoryDebugH
18 #define ZenMemoryDebugH
19 //---------------------------------------------------------------------------
20 
21 //---------------------------------------------------------------------------
22 #if defined(ZENLIB_DEBUG)
23 //---------------------------------------------------------------------------
24 #include "ZenLib/Conf.h"
25 #include <fstream>
26 #include <map>
27 #include <stack>
28 #include <string>
29 //---------------------------------------------------------------------------
30 
31 namespace ZenLib
32 {
33 
34 //***************************************************************************
35 // Class
36 //***************************************************************************
37 
38 class MemoryDebug
39 {
40 public :
41  ~MemoryDebug();
42  static MemoryDebug& Instance();
43 
44  void* Allocate(std::size_t Size, const char* File, int Line, bool Array);
45  void Free(void* Ptr, bool Array);
46  void NextDelete(const char*, int Line); //Sauvegarde les infos sur la désallocation courante
47 
48  void ReportLeaks();
49 
50 private :
51  MemoryDebug();
52  struct TBlock
53  {
54  std::size_t Size; // Taille allouée
55  std::string File; // Fichier contenant l'allocation
56  int Line; // Ligne de l'allocation
57  bool Array; // Est-ce un objet ou un tableau ?
58  };
59  typedef std::map<void*, TBlock> TBlockMap;
60 
61  TBlockMap m_Blocks; // Blocs de mémoire alloués
62  std::stack<TBlock> m_DeleteStack; // Pile dont le sommet contient la ligne et le fichier de la prochaine désallocation
63 };
64 
65 } //NameSpace
66 
67 //***************************************************************************
68 // operator overloadings
69 //***************************************************************************
70 
71 inline void* operator new(std::size_t Size, const char* File, int Line)
72 {
73  return ZenLib::MemoryDebug::Instance().Allocate(Size, File, Line, false);
74 }
75 inline void* operator new[](std::size_t Size, const char* File, int Line)
76 {
77  return ZenLib::MemoryDebug::Instance().Allocate(Size, File, Line, true);
78 }
79 
80 inline void operator delete(void* Ptr)
81 {
82  ZenLib::MemoryDebug::Instance().Free(Ptr, false);
83 }
84 
85 inline void operator delete[](void* Ptr)
86 {
87  ZenLib::MemoryDebug::Instance().Free(Ptr, true);
88 }
89 
90 #if !defined(__BORLANDC__) // Borland does not support overloaded delete
91 inline void operator delete(void* Ptr, const char* File, int Line)
92 {
93  ZenLib::MemoryDebug::Instance().NextDelete(File, Line);
94  ZenLib::MemoryDebug::Instance().Free(Ptr, false);
95 }
96 
97 inline void operator delete[](void* Ptr, const char* File, int Line)
98 {
99  ZenLib::MemoryDebug::Instance().NextDelete(File, Line);
100  ZenLib::MemoryDebug::Instance().Free(Ptr, true);
101 }
102 #endif
103 
104 #if !defined(__MINGW32__) //TODO: Does not work on MinGW, don't know why
105 #ifndef new
106  #define new new(__FILE__, __LINE__)
107 #endif
108 #ifndef delete
109  #define delete ZenLib::MemoryDebug::Instance().NextDelete(__FILE__, __LINE__), delete
110 #endif
111 #endif // __MINGW32__
112 
113 #endif // defined(ZENLIB_DEBUG)
114 
115 #endif // ZenMemoryDebugH