forked from maniacs-satm/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart_pointers.cpp
More file actions
89 lines (81 loc) · 2.13 KB
/
smart_pointers.cpp
File metadata and controls
89 lines (81 loc) · 2.13 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
#include <iostream>
#include <memory>
class Test
{
public:
Test() {}
Test(int i_) : m_i(i_) {}
int getID() const { return 1; }
private:
int m_i;
};
// std::unique_ptr
void challenge1()
{
std::unique_ptr<Test> u1{ new Test };
// copy ctor in unique_ptr = delete
// std::unique_ptr<Test> u2{ u1 };
// Move rights to u2
std::unique_ptr<Test> u2{ std::move( u1 ) };
// Set or get data from ptr
std::cout << "ID of u2 is " << (*u2).getID() << std::endl;
std::cout << "ID of u2 is " << u2.get()->getID() << std::endl;
std::cout << "ID of u2 is " << u2->getID() << std::endl;
Test* t1 = new Test(1);
std::unique_ptr<Test> u3{ t1 };
// We dont have memory leak,
// Now ``u3``s deleter is will be called
u3.reset();
std::cout << " End of challenge1. Now all dtors will be called" << std::endl;
}
// std::shared_ptr
void challenge2()
{
Test* t1 = new Test(1);
std::shared_ptr<Test> s1{ t1 };
std::cout << "Use count in s1: " << s1.use_count() << std::endl;
std::shared_ptr<Test> s2{ s1 };
std::cout << "Use count in s2: " << s2.use_count() << std::endl;
// Move owner to a new shared ptr
// s2 has 0 references
std::shared_ptr<Test> s3{ std::move(s2) };
std::cout << "Use count in s3: " << s3.use_count() << std::endl;
if(!s2)
{
std::cout << "s2 has 0 references" << std::endl;
}
}
// std::weak_ptr
struct B;
struct A
{
A() { std::cout << "A()" << std::endl; }
~A() { std::cout << "~A()" << std::endl; }
std::shared_ptr<B> m_b;
};
struct B
{
B() { std::cout << "B()" << std::endl; }
~B() { std::cout << "~B()" << std::endl; }
// Implementation with memory leak
// std::shared_ptr<A> m_a;
std::weak_ptr<A> m_a;
};
void challenge3()
{
// shared_ptr race condition
// A lock B
// B lock A
// So its a memory leak and A's and B's dtoc won't be called
auto ao = std::make_shared<A>();
ao->m_b = std::make_shared<B>();
// when m_a is a weak_ptr, everything fine
ao->m_b->m_a = ao;
}
int main(int argc, char const *argv[])
{
challenge1();
challenge2();
challenge3();
return 0;
}