aboutsummaryrefslogtreecommitdiff
path: root/CST 126/UnitTests/crt_check_memory.hpp
blob: 9cb4ab2cd4129e1c3ce68755a8f813ce85b58589 (plain) (blame)
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
#ifndef CRTCHECKMEMORY_H
#define CRTCHECKMEMORY_H

#include "pch.h"
#include "CppUnitTest.h"
#include "crtdbg.h"

#define _CRTDBG_MAP_ALLOC

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace LinkedListUnitTests
{
	struct CrtCheckMemory
	{
		_CrtMemState state1{};
		_CrtMemState state2{};
		_CrtMemState state3{};
		CrtCheckMemory()
		{
			_CrtMemCheckpoint(&state1);
		}

		CrtCheckMemory(const CrtCheckMemory& copy) = delete;
		CrtCheckMemory& operator=(const CrtCheckMemory& copy) = delete;
		CrtCheckMemory(CrtCheckMemory&& copy) = delete;
		CrtCheckMemory& operator=(CrtCheckMemory&& copy) = delete;

		~CrtCheckMemory()
		{
			_CrtMemCheckpoint(&state2);

			if (_CrtMemDifference(&state3, &state1, &state2) != 0)
			{
				Assert::Fail(L"Detected memory leaks!");
			}
		}
	};
}
#endif