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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#include <iostream>
#include <memory>
#include <vector>
#include <map>
#include <string>
#include <iomanip>
#include <mutex>
class MemoryTracker
{
private:
struct AllocationInfo
{
size_t size;
std::string file;
int line;
std::string function;
AllocationInfo() = default;
AllocationInfo(size_t s, const std::string& f, int l, const std::string& func)
: size(s), file(f), line(l), function(func) {}
};
static std::map<void*, AllocationInfo> s_Allocations;
static size_t s_TotalAllocated;
static size_t s_TotalFreed;
static size_t s_AllocationCount;
static size_t s_FreeCount;
static std::mutex s_Mutex;
public:
static void* Allocate(size_t size, const char* file = nullptr, int line = 0, const char* function = nullptr)
{
std::lock_guard<std::mutex> lock(s_Mutex);
void* ptr = malloc(size);
if (ptr)
{
s_TotalAllocated += size;
s_AllocationCount++;
if (file && function)
{
s_Allocations[ptr] = AllocationInfo(size, file, line, function);
}
else
{
s_Allocations[ptr] = AllocationInfo(size, "unknown", 0, "unknown");
}
std::cout << "[ALLOC] " << size << " bytes at " << ptr;
if (file && function)
{
std::cout << " (" << file << ":" << line << " in " << function << ")";
}
std::cout << std::endl;
}
return ptr;
}
static void Deallocate(void* ptr, size_t size = 0)
{
if (!ptr) return;
std::lock_guard<std::mutex> lock(s_Mutex);
auto it = s_Allocations.find(ptr);
if (it != s_Allocations.end())
{
size_t allocatedSize = it->second.size;
s_TotalFreed += allocatedSize;
s_FreeCount++;
std::cout << "[FREE ] " << allocatedSize << " bytes at " << ptr
<< " (" << it->second.file << ":" << it->second.line
<< " in " << it->second.function << ")" << std::endl;
s_Allocations.erase(it);
}
else
{
std::cout << "[FREE ] Unknown allocation at " << ptr << std::endl;
}
free(ptr);
}
static void PrintStatistics()
{
std::lock_guard<std::mutex> lock(s_Mutex);
std::cout << "\n=== Memory Statistics ===" << std::endl;
std::cout << "Total allocated: " << s_TotalAllocated << " bytes" << std::endl;
std::cout << "Total freed: " << s_TotalFreed << " bytes" << std::endl;
std::cout << "Current usage: " << (s_TotalAllocated - s_TotalFreed) << " bytes" << std::endl;
std::cout << "Allocation count: " << s_AllocationCount << std::endl;
std::cout << "Free count: " << s_FreeCount << std::endl;
std::cout << "Outstanding allocations: " << s_Allocations.size() << std::endl;
}
static void PrintLeaks()
{
std::lock_guard<std::mutex> lock(s_Mutex);
if (s_Allocations.empty())
{
std::cout << "\n=== No Memory Leaks Detected ===" << std::endl;
return;
}
std::cout << "\n=== Memory Leaks Detected ===" << std::endl;
std::cout << "Outstanding allocations:" << std::endl;
for (const auto& [ptr, info] : s_Allocations)
{
std::cout << " " << info.size << " bytes at " << ptr
<< " (" << info.file << ":" << info.line
<< " in " << info.function << ")" << std::endl;
}
}
static void Reset()
{
std::lock_guard<std::mutex> lock(s_Mutex);
s_Allocations.clear();
s_TotalAllocated = 0;
s_TotalFreed = 0;
s_AllocationCount = 0;
s_FreeCount = 0;
}
};
// 静态成员定义
std::map<void*, MemoryTracker::AllocationInfo> MemoryTracker::s_Allocations;
size_t MemoryTracker::s_TotalAllocated = 0;
size_t MemoryTracker::s_TotalFreed = 0;
size_t MemoryTracker::s_AllocationCount = 0;
size_t MemoryTracker::s_FreeCount = 0;
std::mutex MemoryTracker::s_Mutex;
// 宏定义用于自动捕获文件名、行号和函数名
#define TRACKED_NEW(size) MemoryTracker::Allocate(size, __FILE__, __LINE__, __FUNCTION__)
#define TRACKED_DELETE(ptr) MemoryTracker::Deallocate(ptr)
// 重载全局new和delete(可选)
void* operator new(size_t size)
{
return MemoryTracker::Allocate(size);
}
void operator delete(void* ptr) noexcept
{
MemoryTracker::Deallocate(ptr);
}
void operator delete(void* ptr, size_t size) noexcept
{
MemoryTracker::Deallocate(ptr, size);
}
void DetailedTrackingDemo()
{
std::cout << "=== Detailed Memory Tracking Demo ===" << std::endl;
MemoryTracker::Reset();
// 正常分配和释放
{
int* arr = static_cast<int*>(TRACKED_NEW(sizeof(int) * 10));
TRACKED_DELETE(arr);
}
// 使用智能指针
{
auto ptr = std::make_unique<int>(42);
// 自动释放
}
// 故意制造内存泄漏
{
int* leak = static_cast<int*>(TRACKED_NEW(sizeof(int) * 5));
// 故意不释放
}
MemoryTracker::PrintStatistics();
MemoryTracker::PrintLeaks();
}
int main()
{
DetailedTrackingDemo();
return 0;
}
|