Skip to content

Commit

Permalink
CRT memory leak detection is now available only on Windows systems.
Browse files Browse the repository at this point in the history
This is made so that any data structure can be used in non-Windows systems without conflicts.

Signed-off-by: unknown <saq10002@iteb-219.ad.engr.uconn.edu>
  • Loading branch information
saq10002 committed Oct 10, 2014
1 parent dc7bd3a commit 5c038bf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
17 changes: 15 additions & 2 deletions YASI_12/common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once
//////////////// CRT memory leak detection /////////////

#ifdef _WIN32
//////////////// On Windows, CRT memory leak detection /////////////

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
Expand All @@ -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 <cassert>
#include "test.h"

Expand Down
7 changes: 3 additions & 4 deletions YASI_12/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions YASI_12/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(){}
Expand Down

0 comments on commit 5c038bf

Please sign in to comment.