From 1976ba18878f09b7f7175dd772c98bc0943dcb8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20=27Morty=27=20Str=C3=BCbe?= Date: Fri, 3 Nov 2017 10:55:07 +0100 Subject: [PATCH] Add support for gcc / libc --- gtest_mem_main.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- test/fixture.cpp | 2 +- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/gtest_mem_main.cpp b/gtest_mem_main.cpp index a414a6b..2f1eea2 100644 --- a/gtest_mem_main.cpp +++ b/gtest_mem_main.cpp @@ -1,6 +1,6 @@ /** * @file - * @copyright (c) 2013 Stephan Brenner + * @copyright (c) 2016 Stephan Brenner, Moritz 'Morty' Strübe * @license This project is released under the MIT License. * * This file implements a main() function for Google Test that runs all tests @@ -8,12 +8,14 @@ */ #include -#include #include using namespace std; using namespace testing; +#ifdef _MSC_VER +#include + namespace testing { class MemoryLeakDetector : public EmptyTestEventListener @@ -44,6 +46,42 @@ namespace testing }; } +#elif __GNUC__ +#include + +namespace testing +{ + class MemoryLeakDetector: public EmptyTestEventListener { + + public: + #ifdef _DEBUG + virtual void OnTestStart(const TestInfo&) { + struct mallinfo mi; + mi = mallinfo(); + memusage = mi.uordblks; + } + + virtual void OnTestEnd(const TestInfo& test_info) { + if (test_info.result()->Passed()) { + struct mallinfo mi; + mi = mallinfo(); + int diffResult = mi.uordblks - memusage ; + if (diffResult) { + FAIL()<<"Memory leak of " << diffResult << " byte(s) detected."; + } + } + } + + private: + int memusage; + #endif // _DEBUG + }; +} + +#else +#error No support for your system +#endif + GTEST_API_ int main(int argc, char **argv) { cout << "Running main() from gtest_mld_main.cpp" << endl; diff --git a/test/fixture.cpp b/test/fixture.cpp index 3184e61..1b1ee17 100644 --- a/test/fixture.cpp +++ b/test/fixture.cpp @@ -6,7 +6,7 @@ * This file contains test cases for the gtest_memory_leak_detector. */ -#include +#include #include TEST(MemCheck, MemoryLeak)