From b1835e1de9b0e321953942de19c31aa85b00b7f2 Mon Sep 17 00:00:00 2001 From: philsquared Date: Fri, 17 Feb 2017 23:43:31 +0000 Subject: [PATCH] Moved WIndows leak detector code out of main() --- include/catch.hpp | 23 +++++++++++++++++++++++ include/internal/catch_default_main.hpp | 16 +--------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/include/catch.hpp b/include/catch.hpp index 4bf81da3..72084c37 100644 --- a/include/catch.hpp +++ b/include/catch.hpp @@ -50,6 +50,29 @@ #endif #ifdef CATCH_IMPL + +// !TBD: Move the leak detector code into a separate header +#ifdef CATCH_CONFIG_WINDOWS_CRTDBG +#include +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_default_main.hpp b/include/internal/catch_default_main.hpp index 466efa46..a0c4239d 100644 --- a/include/internal/catch_default_main.hpp +++ b/include/internal/catch_default_main.hpp @@ -10,23 +10,9 @@ #ifndef __OBJC__ -#ifdef CATCH_CONFIG_WINDOWS_CRTDBG -#include -#endif - // Standard C/C++ main entry point int main (int argc, char * argv[]) { -#ifdef CATCH_CONFIG_WINDOWS_CRTDBG - 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); -#endif - int result = Catch::Session().run( argc, argv ); + int result = Catch::Session().run( argc, argv ); return ( result < 0xff ? result : 0xff ); }