29 #include <agrum/agrum.h> 39 #ifndef DOXYGEN_SHOULD_SKIP_THIS 45 # ifdef GUM_DEBUG_MODE 48 typedef std::map< std::string,
int > DEBUG_MAP;
50 static std::mutex debug_mutex;
53 static DEBUG_MAP& sizeof__() {
55 static DEBUG_MAP* sizeOf =
new DEBUG_MAP();
60 static DEBUG_MAP& creation__() {
62 static DEBUG_MAP* creation =
new DEBUG_MAP();
66 static DEBUG_MAP& deletion__() {
68 static DEBUG_MAP* deletion =
new DEBUG_MAP();
72 std::string getFile__(
const char* f) {
74 return s.erase(0, s.rfind(
"/") + 1);
77 void show_trace__(
const char* zeKey,
82 # ifdef GUM_DEEP_TRACE_ON 83 std::cerr << std::setw(40) << std::setfill(
' ') << getFile__(zeFile) <<
"#" 84 << std::setfill(
'0') << std::setw(5) << std::dec << zeLine <<
" : " 85 << zeMsg <<
" <" << zeKey <<
"> [" << std::hex << zePtr <<
"]" 86 << std::dec << std::endl;
90 void inc_creation__(
const char* zeKey,
97 show_trace__(zeKey, zeFile, zeLine, zeMsg, zePtr);
98 creation__()[zeKey]++;
99 sizeof__()[zeKey] = zeSize;
100 debug_mutex.unlock();
104 void dec_creation__(
const char* zeKey,
110 show_trace__(zeKey, zeFile, zeLine, zeMsg, zePtr);
111 creation__()[zeKey]--;
112 debug_mutex.unlock();
115 void inc_deletion__(
const char* zeKey,
121 show_trace__(zeKey, zeFile, zeLine, zeMsg, zePtr);
122 deletion__()[zeKey]++;
123 debug_mutex.unlock();
126 void dumpObjects__() {
128 double total_size = 0.0;
131 int widthColLibelle = 50;
132 int widthColSizeOf = 5;
133 int widthColItemsNumber = 8;
135 std::cout << std::setfill(
'=') <<
"|" << std::setw(widthColLibelle + 2) <<
"" 136 <<
"|" << std::setw(widthColSizeOf + 4) <<
"" 137 <<
"|" << std::setw(widthColItemsNumber + 2) <<
"" 138 <<
"|" << std::setw(widthColItemsNumber + 2) <<
"" 140 std::cout << std::setfill(
' ') <<
"| " << std::left
141 << std::setw(widthColLibelle) <<
"Class Name" << std::right
142 <<
" | " << std::setw(widthColSizeOf) <<
"Size" 143 <<
" | " << std::setw(widthColItemsNumber) <<
"#Const" 144 <<
" | " << std::setw(widthColItemsNumber) <<
"#Dest" 145 <<
" |" << std::endl;
146 std::cout << std::setfill(
'-') <<
"|" << std::setw(widthColLibelle + 2) <<
"" 147 <<
"|" << std::setw(widthColSizeOf + 4) <<
"" 148 <<
"|" << std::setw(widthColItemsNumber + 2) <<
"" 149 <<
"|" << std::setw(widthColItemsNumber + 2) <<
"" 152 std::map< std::string, std::string > res;
154 for (DEBUG_MAP::const_iterator xx = creation__().begin();
155 xx != creation__().end();
157 std::stringstream stream;
158 int zeCreatedObjs = xx->second;
159 int zeDeletedObjts = -1;
160 int size = sizeof__()[xx->first];
162 stream << std::setfill(fillChar = (fillChar ==
'_') ?
' ' :
'_') <<
"| " 163 << std::setw(widthColLibelle) << std::left << xx->first <<
" | " 164 << std::right << std::setw(widthColSizeOf) << size <<
" o | " 165 << std::setw(widthColItemsNumber) << zeCreatedObjs <<
" | ";
167 if (size > 0) total_size += zeCreatedObjs * (size / 1024.0);
170 zeDeletedObjts = deletion__()[xx->first];
171 stream << std::setfill(fillChar) << std::setw(widthColItemsNumber)
173 }
catch (NotFound&) {
174 stream << std::setfill(fillChar) << std::setw(widthColItemsNumber)
181 if (zeCreatedObjs != zeDeletedObjts) {
182 nb_err += std::abs(zeDeletedObjts - zeCreatedObjs);
183 stream <<
"<--- failed";
186 res.insert(make_pair(xx->first, stream.str()));
191 for (DEBUG_MAP::const_iterator xx = deletion__().begin();
192 xx != deletion__().end();
195 creation__()[xx->first];
196 }
catch (NotFound&) {
197 std::stringstream stream;
198 fillChar = (fillChar ==
'_') ?
' ' :
'_';
199 stream << std::setfill(fillChar = (fillChar ==
'_') ?
' ' :
'_') <<
"| " 200 << std::setw(widthColLibelle) << std::left << xx->first +
" " 201 <<
" | " << std::right << std::setw(widthColSizeOf)
202 << sizeof__()[xx->first] <<
" o | " 203 << std::setw(widthColItemsNumber) <<
"?????" 204 <<
" | " << std::setw(widthColItemsNumber) << xx->second
206 res.insert(make_pair(xx->first, stream.str()));
208 nb_err += xx->second;
212 for (
const auto iter: res) {
213 std::cout << iter.second << std::endl;
216 std::cout << std::setfill(
'-');
218 std::cout <<
"|-" << std::setw(widthColLibelle) <<
"" 219 <<
"-|-" << std::setw(widthColSizeOf + 2) <<
"" 220 <<
"-|-" << std::setw(widthColItemsNumber) <<
"" 221 <<
"-|-" << std::setw(widthColItemsNumber) <<
"" 222 <<
"-|" << std::endl;
224 std::cout << std::setfill(
' ');
227 std::cout <<
"| " << std::setw(widthColLibelle) <<
"NO MEMORY LEAK !" 229 << std::setw(widthColSizeOf + widthColItemsNumber * 2 + 9) <<
"" 232 std::cout <<
"| " << std::setw(widthColLibelle) <<
"Memory leaks found " 235 << std::setw(widthColSizeOf + widthColItemsNumber * 2 - 6)
236 << nb_err <<
" object(s) " 240 std::cout <<
"| " << std::setw(widthColLibelle) <<
"total " 241 <<
" | " << std::fixed
242 << std::setw(widthColSizeOf + widthColItemsNumber * 2 - 4)
243 << std::setprecision(2) << total_size <<
" Ko " 246 std::cout << std::setfill(
'=') <<
"|" << std::setw(widthColLibelle + 2) <<
"" 247 <<
"|" << std::setw(widthColSizeOf + widthColItemsNumber * 2 + 10)
254 void staticCorrections__() {}
257 staticCorrections__();
259 creation__().clear();
260 deletion__().clear();