-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleakdetector.h
76 lines (58 loc) · 1.31 KB
/
leakdetector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef LEAKDETECTOR_H
#define LEAKDETECTOR_H
//#define REL_MODE
#ifdef REL_MODE
/// ## FOR RELEASE
#define CHK_LEAK()
#define TRACK_LEAK(Tp)
#else
/// ## FOR DEBUGING
#include <vector>
#include <string>
#include <assert.h>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#define CHK_LEAK() Dbg::LeakDbg::checking = true;
#define TRACK_LEAK(Tp) Dbg::LeakDetector<Tp> leak_obj = Dbg::LeakDetector<Tp>(this);
namespace Dbg {
void checkLeakStack();
struct Pair
{
std::string name;
void* ref;
bool operator==( const Pair &other ) const { return ref == other.ref; }
};
class LeakDbg
{
private:
LeakDbg();
public:
static LeakDbg& instance();
static bool checking;
static std::vector<Pair> stack;
static void addRef(const std::string& nm, void* ptr);
static void remRef(void* ptr);
};
void checkLeakStack();
template <typename Tp>
class LeakDetector {
private:
Tp *objRef;
public:
LeakDetector( Tp *t):objRef(t) {
LeakDbg::instance(); // singlton
LeakDbg::addRef( typeid(objRef).name(), objRef);
}
LeakDetector(const LeakDetector<Tp> &)
{
LeakDbg::instance(); // singlton
LeakDbg::addRef(typeid(Tp).name(), this);
}
~LeakDetector() {
LeakDbg::remRef(objRef);
}
};
}
#endif /// REL_MODE
#endif // LEAKDETECTOR_H