-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
56 lines (45 loc) · 1.41 KB
/
main.cpp
File metadata and controls
56 lines (45 loc) · 1.41 KB
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
#include "cv_memory_pool.h"
#include <iostream>
#include <vector>
#include <thread>
void simple_test() {
std::cout << "=== Simple Test ===" << std::endl;
void* p1 = cvAlloc(20); // 应该进入 32B 子池
void* p2 = cvAlloc(500); // 应该进入 512B 子池
void* p3 = cvAlloc(2000); // 应该进入 4096B 子池
void* p4 = cvAlloc(20000);// 超过最大池,回退 malloc
cvFree(p1);
cvFree(p2);
// p3 故意不释放,制造泄漏
cvFree(p4);
std::cout << "=== Simple Test Done ===" << std::endl;
}
void multi_thread_test() {
std::cout << "=== Multi-thread Test ===" << std::endl;
auto worker = [](int id) {
std::vector<void*> ptrs;
for (int i = 0; i < 100; i++) {
void* p = cvAlloc(64);
ptrs.push_back(p);
}
for (int i = 0; i < 50; i++) {
cvFree(ptrs[i]); // 释放一半
}
// 剩下 50 个泄漏
std::cout << "Thread " << id << " done." << std::endl;
};
std::thread t1(worker, 1);
std::thread t2(worker, 2);
t1.join();
t2.join();
std::cout << "=== Multi-thread Test Done ===" << std::endl;
}
int main() {
simple_test();
multi_thread_test();
std::cout << "=== Main Exit ===" << std::endl;
// 不手动调用 cvReportLeaks(),因为 atexit 会自动输出
// 你可以加上手动调用观察效果:
// cvReportLeaks();
return 0;
}