Fixed a potential ordering issue with output streambufs

This commit is contained in:
Phil Nash 2011-05-31 18:47:30 +01:00
parent 6b6143ccd4
commit 48112c2700
3 changed files with 16 additions and 20 deletions

View File

@ -16,7 +16,6 @@
Later: Later:
Finish command line parser (list as xml, specify FP tolerance) Finish command line parser (list as xml, specify FP tolerance)
Revisit Approx()
Tags? Tags?
Finish macros, listed here, later (just CHECK_NOFAIL now) Finish macros, listed here, later (just CHECK_NOFAIL now)
*/ */

View File

@ -60,15 +60,16 @@ namespace Catch
m_listSpec( List::None ), m_listSpec( List::None ),
m_shouldDebugBreak( false ), m_shouldDebugBreak( false ),
m_showHelp( false ), m_showHelp( false ),
m_streambuf( std::cout.rdbuf() ), m_streambuf( NULL ),
m_os( m_streambuf ), m_os( std::cout.rdbuf() ),
m_includeWhat( Include::FailedOnly ) m_includeWhat( Include::FailedOnly )
{} {}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
~Config() ~Config()
{ {
setStreamBuf( NULL ); m_os.rdbuf( std::cout.rdbuf() );
delete m_streambuf;
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -210,18 +211,16 @@ namespace Catch
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
void setStreamBuf( std::streambuf* buf ) void setStreamBuf( std::streambuf* buf )
{ {
// Delete previous stream buf if we own it
if( m_streambuf && dynamic_cast<StreamBufBase*>( m_streambuf ) )
delete m_streambuf;
m_streambuf = buf;
m_os.rdbuf( buf ? buf : std::cout.rdbuf() ); m_os.rdbuf( buf ? buf : std::cout.rdbuf() );
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
void useStream( const std::string& streamName ) void useStream( const std::string& streamName )
{ {
setStreamBuf( Hub::createStreamBuf( streamName ) ); std::streambuf* newBuf = Hub::createStreamBuf( streamName );
setStreamBuf( newBuf );
delete m_streambuf;
m_streambuf = newBuf;
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////

View File

@ -20,7 +20,6 @@
Later: Later:
Finish command line parser (list as xml, specify FP tolerance) Finish command line parser (list as xml, specify FP tolerance)
Revisit Approx()
Tags? Tags?
Finish macros, listed here, later (just CHECK_NOFAIL now) Finish macros, listed here, later (just CHECK_NOFAIL now)
*/ */
@ -3163,15 +3162,16 @@ namespace Catch
m_listSpec( List::None ), m_listSpec( List::None ),
m_shouldDebugBreak( false ), m_shouldDebugBreak( false ),
m_showHelp( false ), m_showHelp( false ),
m_streambuf( std::cout.rdbuf() ), m_streambuf( NULL ),
m_os( m_streambuf ), m_os( std::cout.rdbuf() ),
m_includeWhat( Include::FailedOnly ) m_includeWhat( Include::FailedOnly )
{} {}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
~Config() ~Config()
{ {
setStreamBuf( NULL ); m_os.rdbuf( std::cout.rdbuf() );
delete m_streambuf;
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -3313,18 +3313,16 @@ namespace Catch
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
void setStreamBuf( std::streambuf* buf ) void setStreamBuf( std::streambuf* buf )
{ {
// Delete previous stream buf if we own it
if( m_streambuf && dynamic_cast<StreamBufBase*>( m_streambuf ) )
delete m_streambuf;
m_streambuf = buf;
m_os.rdbuf( buf ? buf : std::cout.rdbuf() ); m_os.rdbuf( buf ? buf : std::cout.rdbuf() );
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
void useStream( const std::string& streamName ) void useStream( const std::string& streamName )
{ {
setStreamBuf( Hub::createStreamBuf( streamName ) ); std::streambuf* newBuf = Hub::createStreamBuf( streamName );
setStreamBuf( newBuf );
delete m_streambuf;
m_streambuf = newBuf;
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////