unique_ptr in functions
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
#include <memory>
#include <iostream>
class MyType
{
public:
MyType() { std::cout << "MyType::MyType\n"; }
~MyType() { std::cout << "MyType::~MyType\n"; }
};
bool process() { return true; }
bool processSecond() { return true; }
void FuncMightLeak()
{
std::cout << "FuncMightLeak\n";
MyType* pFirst = new MyType();
if (!process())
{
delete pFirst;
return;
}
MyType* pSecond = new MyType();
if (!processSecond())
{
delete pFirst;
delete pSecond;
return;
}
process();
delete pFirst;
delete pSecond;
}
void FuncNoLeaks()
{
std::cout << "FuncNoLeaks\n";
auto pFirst = std::make_unique<MyType>();
if (!process())
return;
auto pSecond = std::make_unique<MyType>();
if (!processSecond())
return;
Enter to Rename, Shift+Enter to Preview