From 5c038bf57b2b4de2f33ab776b5a71a1115503c71 Mon Sep 17 00:00:00 2001 From: saq10002 Date: Fri, 10 Oct 2014 17:20:42 -0400 Subject: [PATCH] CRT memory leak detection is now available only on Windows systems. This is made so that any data structure can be used in non-Windows systems without conflicts. Signed-off-by: unknown --- YASI_12/common.h | 17 +++++++++++++++-- YASI_12/main.cpp | 7 +++---- YASI_12/test.h | 6 ++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/YASI_12/common.h b/YASI_12/common.h index d65b01f..831c447 100644 --- a/YASI_12/common.h +++ b/YASI_12/common.h @@ -1,5 +1,8 @@ #pragma once -//////////////// CRT memory leak detection ///////////// + +#ifdef _WIN32 +//////////////// On Windows, CRT memory leak detection ///////////// + #define _CRTDBG_MAP_ALLOC #include #include @@ -11,7 +14,17 @@ #define new DBG_NEW #endif #endif // _DEBUG -///////////////////////////////////////////////////////// + +// enable dumping memory leaks on Windows +#define DETECT_MEM_LEAKS() \ + _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF) + +#else // Non-Windows systems + +// no support for memory leak detection on Non-Windows systems +#define DETECT_MEM_LEAKS() + +#endif // _WIN32 #include #include "test.h" diff --git a/YASI_12/main.cpp b/YASI_12/main.cpp index 67808a8..30f4fe3 100644 --- a/YASI_12/main.cpp +++ b/YASI_12/main.cpp @@ -29,15 +29,14 @@ using namespace std; void testSort(); int main(int argc, char** argv){ - // enable dumping memory leaks - _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); + + // currently only supported on Windows systems + DETECT_MEM_LEAKS(); ::testing::InitGoogleTest(&argc, argv); // init google test int retVal = RUN_ALL_TESTS(); // run all test cases -// _CrtDumpMemoryLeaks(); - cout << endl; cout << "Press any key..." << endl; getchar(); diff --git a/YASI_12/test.h b/YASI_12/test.h index c73e708..5a2ad18 100644 --- a/YASI_12/test.h +++ b/YASI_12/test.h @@ -8,18 +8,24 @@ namespace yasi{ class Test : public ::testing::Test{ protected: +#ifdef _WIN32 _CrtMemState t1, t2, t3; +#endif virtual void setUp(){ +#ifdef _WIN32 _CrtMemCheckpoint(&t1); +#endif } virtual void tearDown(){ +#ifdef _WIN32 _CrtMemCheckpoint(&t2); if (_CrtMemDifference(&t3, &t1, &t2)) { // _CrtMemDumpStatistics(&t3); // _CrtDumpMemoryLeaks(); _CrtMemDumpAllObjectsSince(&t1); } +#endif } public: virtual ~Test(){}