-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
110 lines (89 loc) · 1.79 KB
/
main.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
// MemControllerService.cpp : Defines the entry point for the application.
//
#include <string>
#include <random>
#include <thread>
#include "MemControllerService.h"
#include "new.hpp"
#include "MemLeakControllerService.h"
using namespace std;
struct test_s
{
test_s(string name_)
: data{}
, name{std::move(name_)}
{ }
int data;
std::string name;
};
inline test_s* create_test_s()
{
std::srand(0);
const auto size = 1 + (rand() % 100);
const char c = 'a' + (rand() % 29);
return new test_s(std::string(size, c));
}
template<typename T, int N> void good()
{
delete (new T);
delete (new(std::nothrow) T);
delete[](new T[N]);
struct Boom { Boom() { throw 1; } };
try { new Boom; }
catch (...) {} // not a leak!
try { new Boom[N]; }
catch (...) {} // same!
}
template<typename T, int N> void mismatch()
{
delete[](new T);
delete (new T[N]);
}
template<typename T, int N> void leak()
{
/*delete*/ new T;
/*delete []*/ new T[N];
/*delete []*/ new T[N][N+2];
}
auto leak_vec()
{
std::vector<test_s*> ivec;
ivec.reserve(5);
for (size_t i = 0; i < 15; i++)
{
ivec.push_back(create_test_s());
}
return ivec;
}
constexpr auto ss = sizeof(void*);
MemLeakControllerService* glMemLeakControllerService = MemLeakControllerService::GetInstance();
int main()
{
//std::set_new_handler(::operator new);
//good<char, 100>();
//mismatch<char, 100>();
//leak<char, 100>();
//auto x = new int[10];
//delete[] x;
//
auto xnt = new(std::nothrow) int(13);
delete xnt;
double* d_ptr{};
std::thread th([&d_ptr]
{
d_ptr = new double(3.14);
});
//std::this_thread::sleep_for(std::chrono::seconds(3));
getchar();
th.join();
delete d_ptr;
//auto ivec = leak_vec();
//ndt::dump_all();
//for (auto& s : ivec)
//{
// delete s;
//}
//ivec.clear();
//ndt::dump_all();
return 0;
}