-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathfinal_release.cpp
More file actions
66 lines (54 loc) · 1.69 KB
/
final_release.cpp
File metadata and controls
66 lines (54 loc) · 1.69 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
#include "pch.h"
using namespace winrt;
using namespace Windows::Foundation;
namespace
{
struct Sample : implements<Sample, IStringable>
{
hstring ToString()
{
return L"Sample";
}
~Sample()
{
// It's safe to QI/AddRef/Release inside destructor.
IStringable s;
check_hresult(QueryInterface(guid_of<IStringable>(), put_abi(s)));
REQUIRE(s.ToString() == L"Sample");
// Weak references are also supported during destruction.
REQUIRE(weak_ref<IStringable>{ s }.get());
REQUIRE(released);
REQUIRE(!destroyed);
destroyed = true;
}
static void final_release(std::unique_ptr<Sample> ptr) noexcept
{
// It's safe to QI/AddRef/Release inside final_release.
IStringable s;
check_hresult(ptr->QueryInterface(guid_of<IStringable>(), put_abi(s)));
REQUIRE(s.ToString() == L"Sample");
// References must be released prior to destroying the unique_ptr.
s = nullptr;
REQUIRE(!released);
REQUIRE(!destroyed);
released = true;
ptr = nullptr;
REQUIRE(destroyed);
}
static inline bool released;
static inline bool destroyed;
};
}
TEST_CASE("final_release")
{
{
auto s = make<Sample>();
// Weak references are supported prior to destruction.
REQUIRE(weak_ref<IStringable>{ s }.get());
REQUIRE(!Sample::released);
REQUIRE(!Sample::destroyed);
s = nullptr;
REQUIRE(Sample::released);
REQUIRE(Sample::destroyed);
}
}