PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

C++, COUT, stdout, _CrtSetReportMode and _CrtSetReportFile

Hi,

I am trying to get the memory leak detection class working which appeared in July 2002 issue of C++ Users Journal.

Apparently it works with Visual C++ and writes to stdout but I have no idea where this output goes with a windows application.

Is there a way to transfer the output to a file?

Anyway, here's the class header file

Code:

// Declarations for the Application Watch Man Class

#if !defined(APPWATCHMAN_H_INCLUDED)
#define APPWATCHMAN_H_INCLUDED

class AppWatchMan
{
public:

typedef enum
{
CHECK_HEAP = 0x01,
TAKE_SNAPSHOT = 0x02,
DUMP_SNAPSHOT = 0x04,
COMP_TO_PREV = 0x08,
COMP_TO_START = 0x10,
LEAKS_FROM_PREV = 0x20,
LEAKS_FROM_START = 0x40,

// Composite Definitions for Scenarios
BEGIN_CONF_CHECK = ( CHECK_HEAP |
TAKE_SNAPSHOT |
DUMP_SNAPSHOT ),
END_CONF_CHECK = ( CHECK_HEAP |
TAKE_SNAPSHOT |
DUMP_SNAPSHOT |
LEAKS_FROM_START ),
BEGIN_LEAK_TEST = ( TAKE_SNAPSHOT | DUMP_SNAPSHOT ),
END_LEAK_TEST = ( TAKE_SNAPSHOT | LEAKS_FROM_PREV |
DUMP_SNAPSHOT),
BEGIN_PROFILE_TEST = ( TAKE_SNAPSHOT | DUMP_SNAPSHOT ),
END_PROFILE_TEST = ( TAKE_SNAPSHOT |
DUMP_SNAPSHOT |
COMP_TO_PREV)
} FLAGS;

AppWatchMan();

virtual ~AppWatchMan(void){};
bool DoRounds(const char* szComment, FLAGS flags);

private:

static const bool m_bOnDuty; // always false for rel. cfg

ostream_withassign* m_pStrLog;

_CrtMemState m_stFirst;
_CrtMemState m_stPrevious;
_CrtMemState m_stLast;
_CrtMemState m_stDiff;

// Logging String Definitions Omitted

};
#endif // !defined(APPWATCHMAN_H_INCLUDED)


and the source
Code:

// Definition for Application Watch Man Class Functions
#include <time.h>
#include <iostream.h>
#include <fstream.h>
#include <stdarg.h>
#include <malloc.h>
#include <crtdbg.h>
#include <memory.h>
#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <string>
#include "AppWatchMan.h"

using namespace std;

// Since _Crt functions are only compiled if _DEBUG
#ifdef _DEBUG
const bool AppWatchMan::m_bOnDuty = true;
#else
const bool AppWatchMan::m_bOnDuty = false;
#endif

AppWatchMan::AppWatchMan() : m_pStrLog(&cout;)
{
if ( m_bOnDuty )
{
ios::sync_with_stdio();

_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE );
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE );
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE );
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);

memset((void *)&m;_stFirst, 0, sizeof(m_stFirst));
memset((void *)&m;_stPrevious, 0, sizeof(m_stPrevious));
memset((void *)&m;_stLast, 0, sizeof(m_stLast));
memset((void *)&m;_stDiff, 0, sizeof(m_stDiff));

time_t ltime;
time( <ime; );
*m_pStrLog << ON_DUTY_STR << ctime(<ime;);

int nHeapOkay = TRUE;
*m_pStrLog << CHECK_HEAP_STR << endl;
_ASSERT(nHeapOkay = _CrtCheckMemory());
if ( nHeapOkay == TRUE )
{
*m_pStrLog << HEAP_IS_OKAY_STR << endl;
}
*m_pStrLog << TAKE_SNAPSHOT_STR << endl;
_CrtMemCheckpoint(&m;_stFirst);
*m_pStrLog << DUMP_SNAPSHOT_STR << endl;
_CrtMemDumpStatistics(&m;_stFirst);
m_stLast = m_stFirst;
}
}

bool AppWatchMan::DoRounds(const char* szComment, FLAGS flags)
{
bool bErrorFound = false;

if ( m_bOnDuty )
{
bool bPotentialLeak = false;
int nHeapOkay = TRUE;

time_t ltime;
time( <ime; );
*m_pStrLog << endl << DO_ROUNDS_STR << szComment << " " \
<< ctime(<ime;);

// Check the heap memory integrity before anything
if ( (flags & CHECK_HEAP) != 0 )
{
*m_pStrLog << CHECK_HEAP_STR << endl;
_ASSERT(nHeapOkay = _CrtCheckMemory());
if ( nHeapOkay == TRUE )
{
*m_pStrLog << HEAP_IS_OKAY_STR << endl;
}
else
{
bErrorFound = true;
}
}

// TAKE_SNAPSHOT
if ( (flags & TAKE_SNAPSHOT) != 0 )
{
// Move Last Reading to Previous
m_stPrevious = m_stLast;

// Take a Reading (Win32 C Run Time Library)
*m_pStrLog << TAKE_SNAPSHOT_STR << endl;
_CrtMemCheckpoint(&m;_stLast);

if ( (flags & DUMP_SNAPSHOT) != 0 )
{
*m_pStrLog << DUMP_SNAPSHOT_STR << endl;
_CrtMemDumpStatistics(&m;_stLast);
}
}

// COMP_TO_PREV
if ( (flags & COMP_TO_PREV) != 0 )
{
bPotentialLeak =
(_CrtMemDifference( &m;_stDiff,
&m;_stPrevious,
&m;_stLast )== TRUE) ? true : false;
if ( bPotentialLeak )
{
bErrorFound = true;
*m_pStrLog << POTENTIAL_LEAK_STR << endl;
}
*m_pStrLog << COMP_TO_PREV_STR << endl;
_CrtMemDumpStatistics(&m;_stDiff);
}

// COMP_TO_START
if ( (flags & COMP_TO_START) != 0 )
{
bPotentialLeak =
(_CrtMemDifference( &m;_stDiff,
&m;_stFirst,
&m;_stLast )== TRUE) ? true : false;
if ( bPotentialLeak )
{
bErrorFound = true;
*m_pStrLog << POTENTIAL_LEAK_STR << endl;
}
*m_pStrLog << COMP_TO_START_STR << endl;
_CrtMemDumpStatistics(&m;_stDiff);
}

// LEAKS_FROM_PREV (only if there is a diff.)
if ( (flags & LEAKS_FROM_PREV) != 0 )
{
bPotentialLeak =
(_CrtMemDifference( &m;_stDiff,
&m;_stPrevious,
&m;_stLast )== TRUE) ? true : false;
if ( bPotentialLeak )
{
bErrorFound = true;
*m_pStrLog << POTENTIAL_LEAK_STR << endl;
*m_pStrLog << LEAKS_FROM_PREV_STR << endl;
_CrtMemDumpAllObjectsSince(&m;_stPrevious);
}
}

// LEAKS_FROM_START
if ( (flags & LEAKS_FROM_START) != 0 )
{
bPotentialLeak =
(_CrtMemDifference( &m;_stDiff,
&m;_stFirst,
&m;_stLast ) == TRUE) ? true : false;
if ( bPotentialLeak )
{
bErrorFound = true;
*m_pStrLog << POTENTIAL_LEAK_STR << endl;
*m_pStrLog << LEAKS_FROM_START_STR << endl;
_CrtMemDumpAllObjectsSince(&m;_stFirst);
}
else
{
*m_pStrLog << NO_LEAKS_STR << " Since First Snapshot!" << endl;
}
}
}
return bErrorFound;
}

// end of AppWatchMan.cpp


I have managed to get comments written to a file using freopen() and the output from _crt functions to the debug window and to it's own window but neither together.

Please help and if I am successful in getting this class to work with a GAPI app I will post the completed working class to this forum.

Thanks in advance
BadBazza
Hello again,

Maybe I should ask the questions more directly:-

1. This class currently writes to cout, under a windows app where would this output go? How do I look at it?

2. Is it possible to create another version of cout which is a file? eg

ostream mycout= .................
cout=mycout;

Thanks in advance
BadBazza