Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow_debug_memory.h
Go to the documentation of this file.
1#pragma once
2#include <format>
3#include <iostream>
4
5// MSVC built-in debug flag
6#if defined _DEBUG && (defined _WIN32 || defined _WIN64)
7#include <stdlib.h>
8#include <crtdbg.h>
9#define _CRTDBG_MAP_ALLOC
10#endif
11
12namespace flow {
15 inline void enableMemoryGuard() {
16 #ifdef _CRTDBG_MAP_ALLOC
17 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
18 #endif
19 }
20
23 class DebugClass {
24 std::shared_ptr<std::size_t> copyCounter_;
25 std::size_t copies_;
26 std::size_t id_;
27
28 public:
30 : copyCounter_(std::make_shared<std::size_t>(0)),
31 copies_(0),
32 id_(globalId++) {
33 std::cout << std::format("default_ctor id {}, copies {}\n", id_, copies_);
34 }
35
39 id_(rhs.id_) {
40
41 std::cout << std::format("copy_ctor id {}, copies {}\n", id_, copies_);
42 }
43
44 DebugClass(DebugClass&& rhs) noexcept
45 : copyCounter_(std::move(rhs.copyCounter_)),
46 copies_(rhs.copies_),
47 id_(rhs.id_) {
48 std::cout << std::format("move_ctor id {}, copies {}\n", id_, copies_);
49 }
50
52 std::cout << std::format("dtor id {}, copies {}\n", id_, copies_);
53 }
54
57 copies_ = ++(*copyCounter_);
58 id_ = rhs.id_;
59 std::cout << std::format("copy_op id {}, copies {}\n", id_, copies_);
60 }
61
62 DebugClass& operator=(DebugClass&& rhs) noexcept {
63 copyCounter_ = std::move(rhs.copyCounter_);
64 copies_ = rhs.copies_;
65 id_ = rhs.id_;
66 std::cout << std::format("move_op id {}, copies {}\n", id_, copies_);
67 }
68
69 private:
70 static inline std::size_t globalId = 0;
71
72 friend std::ostream& operator<<(std::ostream& out, const DebugClass& obj) {
73 out << std::format("{{id: {}, copies: {}}}", obj.id_, obj.copies_);
74 return out;
75 }
76 };
77}
DebugClass & operator=(DebugClass &&rhs) noexcept
DebugClass(const DebugClass &rhs)
DebugClass & operator=(const DebugClass &rhs)
static std::size_t globalId
DebugClass(DebugClass &&rhs) noexcept
friend std::ostream & operator<<(std::ostream &out, const DebugClass &obj)
std::shared_ptr< std::size_t > copyCounter_
void enableMemoryGuard()
Enable MSVC native memory leak checker in debug mode. Not compatible with address sanitizer.