Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Moved leak detector to its own file

Commit
4332b84c9bcf2b1e70a6e6e9c4ce27ade9f8365c
Author
Martin Hořeňovský ·
Parents
9c318af98728

Files · Download .tar.gz

Changes

 CMakeLists.txt                           |  2 ++
 include/catch.hpp                        | 23 ----------------------
 include/internal/catch_impl.hpp          |  4 ++++
 include/internal/catch_leak_detector.cpp | 33 ++++++++++++++++++++++++++++++++
 include/internal/catch_leak_detector.h   | 17 ++++++++++++++++
 5 files changed, 56 insertions(+), 23 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 137aeb8e..0c680adc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -147,6 +147,7 @@ set(INTERNAL_HEADERS
         ${HEADER_DIR}/internal/catch_interfaces_runner.h
         ${HEADER_DIR}/internal/catch_interfaces_tag_alias_registry.h
         ${HEADER_DIR}/internal/catch_interfaces_testcase.h
+        ${HEADER_DIR}/internal/catch_leak_detector.h
         ${HEADER_DIR}/internal/catch_list.h
         ${HEADER_DIR}/internal/catch_matchers.hpp
         ${HEADER_DIR}/internal/catch_matchers_string.h
@@ -202,6 +203,7 @@ set(IMPL_SOURCES
         ${HEADER_DIR}/internal/catch_exception_translator_registry.cpp
         ${HEADER_DIR}/internal/catch_fatal_condition.cpp
         ${HEADER_DIR}/internal/catch_list.cpp
+        ${HEADER_DIR}/internal/catch_leak_detector.cpp
         ${HEADER_DIR}/internal/catch_matchers_string.cpp
         ${HEADER_DIR}/internal/catch_message.cpp
         ${HEADER_DIR}/internal/catch_notimplemented_exception.cpp
diff --git a/include/catch.hpp b/include/catch.hpp
index ccbfd28e..40ee9da1 100644
--- a/include/catch.hpp
+++ b/include/catch.hpp
@@ -50,29 +50,6 @@
 #endif
 
 #ifdef CATCH_IMPL
-
-// !TBD: Move the leak detector code into a separate header
-#ifdef CATCH_CONFIG_WINDOWS_CRTDBG
-#include <crtdbg.h>
-class LeakDetector {
-public:
-	LeakDetector() {
-		int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
-		flag |= _CRTDBG_LEAK_CHECK_DF;
-		flag |= _CRTDBG_ALLOC_MEM_DF;
-		_CrtSetDbgFlag(flag);
-		_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
-		_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
-		// Change this to leaking allocation's number to break there
-		_CrtSetBreakAlloc(-1);
-	}
-};
-#else
-class LeakDetector {};
-#endif
-
-LeakDetector leakDetector;
-
 #include "internal/catch_impl.hpp"
 #endif
 
diff --git a/include/internal/catch_impl.hpp b/include/internal/catch_impl.hpp
index b6249593..637df27b 100644
--- a/include/internal/catch_impl.hpp
+++ b/include/internal/catch_impl.hpp
@@ -21,6 +21,8 @@
 #include "catch_reporter_registrars.hpp"
 //
 
+#include "internal/catch_leak_detector.h"
+
 
 #include "../catch_session.hpp"
 #include "catch_stream.hpp"
@@ -31,6 +33,8 @@
 // ~*~* CATCH_CPP_STITCH_PLACE *~*~
 
 namespace Catch {
+    LeakDetector leakDetector;
+
     // These are all here to avoid warnings about not having any out of line
     // virtual methods
     NonCopyable::~NonCopyable() {}
diff --git a/include/internal/catch_leak_detector.cpp b/include/internal/catch_leak_detector.cpp
new file mode 100644
index 00000000..32109310
--- /dev/null
+++ b/include/internal/catch_leak_detector.cpp
@@ -0,0 +1,33 @@
+/*
+ *  Created by Martin on 12/07/2017.
+ *
+ *  Distributed under the Boost Software License, Version 1.0. (See accompanying
+ *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+ #include "catch_leak_detector.h"
+
+
+namespace Catch {
+
+#ifdef CATCH_CONFIG_WINDOWS_CRTDBG
+#include <crtdbg.h>
+
+	LeakDetector::LeakDetector() {
+		int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
+		flag |= _CRTDBG_LEAK_CHECK_DF;
+		flag |= _CRTDBG_ALLOC_MEM_DF;
+		_CrtSetDbgFlag(flag);
+		_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
+		_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
+		// Change this to leaking allocation's number to break there
+		_CrtSetBreakAlloc(-1);
+	}
+
+#else
+
+    LeakDetector::LeakDetector(){}
+
+#endif
+
+}
diff --git a/include/internal/catch_leak_detector.h b/include/internal/catch_leak_detector.h
new file mode 100644
index 00000000..bfb0b429
--- /dev/null
+++ b/include/internal/catch_leak_detector.h
@@ -0,0 +1,17 @@
+/*
+ *  Created by Martin on 12/07/2017.
+ *
+ *  Distributed under the Boost Software License, Version 1.0. (See accompanying
+ *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+ */
+#ifndef TWOBLUECUBES_CATCH_LEAK_DETECTOR_H_INCLUDED
+#define TWOBLUECUBES_CATCH_LEAK_DETECTOR_H_INCLUDED
+
+namespace Catch {
+
+    struct LeakDetector {
+        LeakDetector();
+    };
+
+}
+#endif // TWOBLUECUBES_CATCH_LEAK_DETECTOR_H_INCLUDED