forked from sunziping2016/MemoryLeakDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_leak_detection.cpp
160 lines (147 loc) · 3.91 KB
/
memory_leak_detection.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include "hashset.h"
#include "log.h"
using namespace std;
struct extra_info {
const char *file;
long line;
bool single;
};
size_t nnew = 0, ndelete = 0, nerror = 0;
hashset set;
bool memory_leak_detect = false;
void memory_leak_detect_on()
{
if (!memory_leak_detect) {
memory_leak_detect = true;
clog << info << "INFO: memory leak detection on" << endl;
}
}
void memory_leak_detect_off()
{
if (memory_leak_detect) {
memory_leak_detect = false;
clog << info << "INFO: memory leak detection off" << endl;
}
}
void memory_leak_detect_clear()
{
nnew = 0;
ndelete = 0;
nerror = 0;
set.clear();
clog << info << "INFO: memory leak detection data clear" << endl;
}
void memory_leak_detect_report()
{
hashset_iter iter = set.iter();
while (iter.has_next()) {
extra_info *einfo = (extra_info *)iter.value();
clog << error << "ERROR: memory leak @" << (einfo + 1) << " (" << einfo->file << ":" << einfo->line << ")" << endl;
++nerror;
iter.next();
}
clog << info << "INFO: " << nnew << " new call(s)\t" << ndelete << " delete call(s)\t" << nerror << " errors" << endl;
}
void * operator new(std::size_t sz)
{
void *ptr = malloc(sizeof(extra_info) + sz);
extra_info *einfo = (extra_info *)ptr;
einfo->file = "";
einfo->line = 0;
einfo->single = true;
if (memory_leak_detect) {
++nnew;
clog << info << "INFO: new @" << (einfo + 1) << " (" << einfo->file << ":" << einfo->line << ")" << endl;
set.add(ptr);
}
return (void *)(einfo + 1);
}
void * operator new[](std::size_t sz)
{
void *ptr = malloc(sizeof(extra_info) + sz);
extra_info *einfo = (extra_info *)ptr;
einfo->file = "";
einfo->line = 0;
einfo->single = false;
if (memory_leak_detect) {
++nnew;
clog << info << "INFO: new[] @" << (einfo + 1) << " (" << einfo->file << ":" << einfo->line << ")" << endl;
set.add(ptr);
}
return (void *)(einfo + 1);
}
void * operator new(std::size_t sz, const char *file, long line)
{
void *ptr = malloc(sizeof(extra_info) + sz);
extra_info *einfo = (extra_info *)ptr;
einfo->file = file;
einfo->line = line;
einfo->single = true;
if (memory_leak_detect) {
++nnew;
clog << info << "INFO: new @" << (einfo + 1) << " (" << einfo->file << ":" << einfo->line << ")" << endl;
set.add(ptr);
}
return (void *)(einfo + 1);
}
void * operator new[](std::size_t sz, const char *file, long line)
{
void *ptr = malloc(sizeof(extra_info) + sz);
extra_info *einfo = (extra_info *)ptr;
einfo->file = file;
einfo->line = line;
einfo->single = false;
if (memory_leak_detect) {
++nnew;
clog << info << "INFO: new[] @" << (einfo + 1) << " (" << einfo->file << ":" << einfo->line << ")" << endl;
set.add(ptr);
}
return (void *)(einfo + 1);
}
void operator delete(void * ptr) noexcept
{
extra_info *einfo = (extra_info *)ptr - 1;
ptr = (void *)einfo;
if (!memory_leak_detect) {
free(ptr);
return;
}
++ndelete;
if (!set.contains(ptr)) {
clog << error << "ERROR: delete wild pointer @" << (einfo + 1) << endl;
++nerror;
}
else {
if (!einfo->single) {
clog << warning << "WARNING: mismatch delete on array pointer @" << (einfo + 1) << " (" << einfo->file << ":" << einfo->line << ")" << endl;
++nerror;
}
clog << info << "INFO: delete @" << (einfo + 1) << " (" << einfo->file << ":" << einfo->line << ")" << endl;
set.remove(ptr);
free(ptr);
}
}
void operator delete[](void * ptr) noexcept
{
extra_info *einfo = (extra_info *)ptr - 1;
ptr = (void *)einfo;
if (!memory_leak_detect) {
free(ptr);
return;
}
++ndelete;
if (!set.contains(ptr)) {
clog << error << "ERROR: delete wild pointer @" << (einfo + 1) << endl;
++nerror;
}
else {
if (einfo->single) {
clog << warning << "WARNING: mismatch delete[] on single-object pointer @" << (einfo + 1) << " (" << einfo->file << ":" << einfo->line << ")" << endl;
++nerror;
}
clog << info << "INFO: delete[] @" << (einfo + 1) << " (" << einfo->file << ":" << einfo->line << ")" << endl;
set.remove(ptr);
free(ptr);
}
}