From d8230a8d4d5fc4a56d28367791f4440b80438a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Thu, 16 Feb 2017 11:09:09 +0100 Subject: [PATCH 1/3] Add opt-in leak detection under MSVC + Windows. Closes #439 --- docs/configuration.md | 3 +++ include/internal/catch_default_main.hpp | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 4cedc72c..c1616db6 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -76,6 +76,7 @@ All C++11 support can be disabled with `CATCH_CONFIG_NO_CPP11` CATCH_CONFIG_WINDOWS_SEH // Enable SEH handling on Windows CATCH_CONFIG_FAST_COMPILE // Sacrifices some (extremely minor) features for compilation speed CATCH_CONFIG_POSIX_SIGNALS // Enable handling POSIX signals + CATCH_CONFIG_WINDOWS_CRTDBG // Enable leak checking using Windows's CRT Debug Heap Currently Catch enables `CATCH_CONFIG_WINDOWS_SEH` only when compiled with MSVC, because some versions of MinGW do not have the necessary Win32 API support. @@ -83,6 +84,8 @@ At this moment, `CATCH_CONFIG_FAST_COMPILE` changes only the behaviour of the `- `CATCH_CONFIG_POSIX_SIGNALS` is on by default, except when Catch is compiled under `Cygwin`, where it is disabled by default (but can be force-enabled by defining `CATCH_CONFIG_POSIX_SIGNALS`). +`CATCH_CONFIG_WINDOWS_CRTDBG` is off by default. If enabled, it sets Windows's CRT to check for memory leaks, and displays them after the tests finish running. + Just as with the C++11 conformance toggles, these toggles can be disabled by using `_NO_` form of the toggle, e.g. `CATCH_CONFIG_NO_WINDOWS_SEH`. # Windows header clutter diff --git a/include/internal/catch_default_main.hpp b/include/internal/catch_default_main.hpp index 12ca9024..2a8b226f 100644 --- a/include/internal/catch_default_main.hpp +++ b/include/internal/catch_default_main.hpp @@ -10,8 +10,22 @@ #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); + _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 ); return ( result < 0xff ? result : 0xff ); } From 9202a7749816043403b8395375ff2ec35ba8e1a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Thu, 16 Feb 2017 11:11:52 +0100 Subject: [PATCH 2/3] Documentation improvement --- docs/configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index c1616db6..9c9c1c0d 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -84,7 +84,7 @@ At this moment, `CATCH_CONFIG_FAST_COMPILE` changes only the behaviour of the `- `CATCH_CONFIG_POSIX_SIGNALS` is on by default, except when Catch is compiled under `Cygwin`, where it is disabled by default (but can be force-enabled by defining `CATCH_CONFIG_POSIX_SIGNALS`). -`CATCH_CONFIG_WINDOWS_CRTDBG` is off by default. If enabled, it sets Windows's CRT to check for memory leaks, and displays them after the tests finish running. +`CATCH_CONFIG_WINDOWS_CRTDBG` is off by default. If enabled, Windows's CRT is used to check for memory leaks, and displays them after the tests finish running. _Note that this instrumentation is only done when Catch supplies its own main._ Just as with the C++11 conformance toggles, these toggles can be disabled by using `_NO_` form of the toggle, e.g. `CATCH_CONFIG_NO_WINDOWS_SEH`. From 30e4dbef1c2a4742a85f5d57c84107631cce9c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Fri, 17 Feb 2017 14:22:42 +0100 Subject: [PATCH 3/3] Report leaks to debugger in addition to stderr This branch should also close #120. --- include/internal/catch_default_main.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/internal/catch_default_main.hpp b/include/internal/catch_default_main.hpp index 2a8b226f..466efa46 100644 --- a/include/internal/catch_default_main.hpp +++ b/include/internal/catch_default_main.hpp @@ -21,7 +21,7 @@ int main (int argc, char * argv[]) { flag |= _CRTDBG_LEAK_CHECK_DF; flag |= _CRTDBG_ALLOC_MEM_DF; _CrtSetDbgFlag(flag); - _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); + _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);