Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost test, segmentation fault.
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2010-08-26 18:09:03


AMDG

Matthijs Mohlmann wrote:
> First of all:
> I'm building an application and with that I use Boost test for unit testing. This helped
> me already to fix some obscure bugs. (I am very happy with boost test)
>
> I have the Master suite test setup in one file and the normal test suites each in one
> file. This worked very well. Now I have the following test suite which gives me a
> segmentation fault and I have no idea why:
>
> BOOST_AUTO_TEST_SUITE(ProgramOptions)
>
> BOOST_AUTO_TEST_CASE(ProgramOptions_Valid_Options) {
>
> // Output
> boost::test_tools::output_test_stream output("misc/ProgramOptions_01.output", true);
>
> // Save output buffer
> std::streambuf *strmbuffer;
> strmbuffer = std::cout.rdbuf();
>
> // Redirect the output.
> std::cout.rdbuf(output.rdbuf());
>
> // Redirect output stream so we can do some checks.
> printSomething();
> BOOST_REQUIRE(output.match_pattern());
>
> // Now lets process some arguments.
> printSomething();
> BOOST_REQUIRE(output.match_pattern());
>
> // Restore output buffer
> std::cout.rdbuf(strmbuffer);
> }
>
> BOOST_AUTO_TEST_SUITE_END()
>
> The function that gets tested:
> void printSomething() {
> std::cout << "Test output" << std::endl;
> }
>
> If I put the same content 'Test output' in the ProgramOptions_01.output file then everything
> is fine and executes ok. When putting different content in that file it goes wrong.
>

The problem is that when BOOST_REQUIRE fails, the
test case immediately aborts, the output buffer is not
restored, the output_test_stream is destroyed, and
std::cout is left with a dangling pointer.

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net