-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (120 loc) · 3.59 KB
/
main.cpp
File metadata and controls
140 lines (120 loc) · 3.59 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
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
// MemoryLeakManagement.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "MemoryManager.h"
#include <thread>
#include <memory>
#include <iostream>
#include <string>
struct MyStruct { double x; double y; };
void MakeNoLeaks() {
auto p1 = std::make_shared<int>();
auto p2 = std::make_shared<int>();
auto p3 = std::make_shared<int>();
p1 = std::make_shared<int>();
p2 = std::make_shared<int>();
p3 = std::make_shared<int>();
auto p4 = new MyStruct();
delete p4;
p4 = new MyStruct();
delete p4;
p4 = new MyStruct();
delete p4;
}
void DetectNoLeaks() {
ResetAllocatedPointers();
MakeNoLeaks();
auto count = CollectGarbage();
std::cout << "\033[37;41m" << "In function " << std::string(__FUNCTION__) << " " << std::to_string(count) << " leaks are detected" << "\033[0m" << std::endl << std::endl;
}
void Leak1000()
{
for (int i = 0; i < 1000; i++)
auto st = new MyStruct();
}
struct MyStructWithPtr
{
double x; double* y;
MyStructWithPtr()
{
x = 10;
y = new double(5);
}
};
void LeakMyStructWithPtr()
{
auto ptr = new MyStructWithPtr();
}
int* GetDenaglingPtr() {
auto ptr = new int(1);
auto ptr1 = ptr;
delete ptr;
return ptr1;
}
MyStructWithPtr* GetDeepDanglingPtr() {
auto ptr = new MyStructWithPtr();
delete ptr->y;
auto doublePtr = new double(10);
ptr->y = doublePtr;
delete doublePtr;
return ptr;
}
void DanglingPointersDetection() {
ResetAllocationList();
auto danglingPtr = GetDenaglingPtr();
auto deepDanglingPtr = GetDeepDanglingPtr();
DetectDanglingPointers();
}
void Detect1000LeaksInCalledFunction() {
ResetAllocationList();
Leak1000();
auto count = CollectGarbage();
std::cout << "\033[37;41m" << "In function " << std::string(__FUNCTION__) << " " << std::to_string(count) << " leaks are detected" << "\033[0m" << std::endl << std::endl;
}
void DetectLeaksInLoop() {
ResetAllocationList();
for (int i = 0; i < 10; i++)
{
auto st1 = new MyStruct();
auto st2 = new MyStruct();
}
auto count = CollectGarbage();
std::cout << "\033[37;41m" << "In function " << std::string(__FUNCTION__) << " " << std::to_string(count) << " leaks are detected" << "\033[0m" << std::endl << std::endl;
}
void DetectLeaksInBlock() {
ResetAllocationList();
{
auto st1 = new MyStruct();
st1 = new MyStruct();
auto st2 = new MyStruct();
st2 = new MyStruct();
}
auto count = CollectGarbage();
std::cout << "\033[37;41m" << "In function " + std::string(__FUNCTION__) + " " + std::to_string(count) + " leaks are detected" << "\033[0m" << std::endl << std::endl;
}
void DetectIndirectLeaks() {
ResetAllocationList();
LeakMyStructWithPtr(); // 2 leaks in here;
MyStructWithPtr myStructWithPtr; // no leak despite allocation in default ctor.
auto ptrMyStructWithPtr = new MyStructWithPtr(); // no leaks despite inner pointer is not in the stack rather in the heap.
auto count = CollectGarbage();
std::cout << "\033[37;41m" << "In function " << std::string(__FUNCTION__) << " " << std::to_string(count) << " leaks are detected" << "\033[0m" << std::endl << std::endl;
}
void DetectLeaksInThread() {
ResetAllocationList();
MyStruct* myStruct = nullptr;
{
std::thread([&myStruct] { myStruct = new MyStruct();
new MyStruct(); /*leak*/ }).join();
}
auto count = CollectGarbage();
std::cout << "\033[37; 41m" << "In function " << std::string(__FUNCTION__) << " " << std::to_string(count) << " leaks are detected" << "\033[0m" << std::endl << std::endl;
}
int main() {
DetectNoLeaks();
DetectLeaksInLoop();
DetectLeaksInBlock();
Detect1000LeaksInCalledFunction();
DetectIndirectLeaks();
DetectLeaksInThread();
DanglingPointersDetection();
}