-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_memory_leak.cpp
88 lines (77 loc) · 2.16 KB
/
test_memory_leak.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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <pthread.h>
using namespace std;
struct CBaseNG {
CBaseNG() {
::printf("Call CBaseNG class constructor.\n");
}
// !!! NOT Virtual: Not call inherit's destructor .
~CBaseNG() {
::printf("Call CBaseNG class destructor.\n");
}
};
struct NG : public CBaseNG
{
NG(const string& s) : CBaseNG(), data() {
::printf("Call NG class constructor.\n");
data = new string(s);
::printf("** memory allocation: %p\n", (void*)data);
}
virtual ~NG() {
// NOT CALL
::printf("Call NG class destructor.\n");
::printf("** memory free: %p\n", (void*)data);
delete data;
}
string* data;
};
void* leak_thread(void* arg)
{
int count = (int)(long)(arg);
for (int i = 0; i < 100; ++i) {
void* p = ::malloc(64+i);
if (i == count) {
// !!! oops..
continue;
}
free(p);
}
return NULL;
}
int main(int argc, char* argv[])
{
if (argc < 2) {
printf("%s [1/2]\n", argv[0]);
return 0;
}
int mode = ::atoi(argv[1]);
if (mode == 1) {
::printf(
"Case 1: Forgotton with a virtual destructor of the base class.\n"
"--------------------------------------------------------------\n"
);
CBaseNG* v1 = new NG("ABC");
void* p = reinterpret_cast<void*>(v1);
::printf("** memory allocation: %p\n", p);
delete v1;
::printf("** memory free: %p\n", p);
} else if (mode == 2) {
::printf(
"Case 2: Not match number of calling malloc and free.\n"
"--------------------------------------------------------------\n"
);
pthread_t t1, t2, t3;
pthread_create(&t1, NULL, leak_thread, (void*)(long)10);
pthread_create(&t2, NULL, leak_thread, (void*)(long)30);
pthread_create(&t3, NULL, leak_thread, (void*)(long)50);
void* res;
pthread_join(t1, &res);
pthread_join(t2, &res);
pthread_join(t3, &res);
}
return 0;
}
// vim: ts=4 sts=4 sw=4 expandtab :