Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions gtest_mem_main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/**
* @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
* and detects memory leaks.
*/

#include <iostream>
#include <crtdbg.h>
#include <gtest/gtest.h>

using namespace std;
using namespace testing;

#ifdef _MSC_VER
#include <crtdbg.h>

namespace testing
{
class MemoryLeakDetector : public EmptyTestEventListener
Expand Down Expand Up @@ -44,6 +46,42 @@ namespace testing
};
}

#elif __GNUC__
#include <malloc.h>

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;
Expand Down
2 changes: 1 addition & 1 deletion test/fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* This file contains test cases for the gtest_memory_leak_detector.
*/

#include <crtdbg.h>
#include <malloc.h>
#include <gtest/gtest.h>

TEST(MemCheck, MemoryLeak)
Expand Down