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(){}