Boost logo

Boost Users :

Subject: Re: [Boost-users] [Iostreams] Segmentation fault when stream is flushed or closed
From: Slava (Viatcheslav.Sysoltsev_at_[hidden])
Date: 2013-11-26 02:55:25


On Mon, 25 Nov 2013 13:21:33 +0100, Steven Maenhout
<Steven.Maenhout_at_[hidden]> wrote:

> On 11/25/2013 12:43 PM, Slava wrote:
>
>> I guess, you cannot pass temporaries into
>> TeeDevice constructor and have to hold dummyFileHandle and logStream2
>> alive as long as they are referenced in TeeDevice (making them class
>> members accessed through shared_ptr perhaps?).
>
> Indeed, that was also my first conclusion but if I explicitly create
> temporary sinks by scoping but then flush and close the stream in the
> same constructor all works fine and Valgrind reports no issues:
>
> {
> std::ofstream dummyFileHandle("/dev/null", std::ios_base::out |
> std::ios_base::app);
> std::stringstream logStream2;
> TeeDevice myTee (logStream2, dummyFileHandle);
> m_out.open(myTee);
> }
>
> if(m_out.is_open()) {
> m_out.flush();
> m_out.close();
> }
>
> In fact, my earlier code also works fine if you do not pass the unused
> logStream to the constructor, which I find very confusing...

Generated assembly for Test::Test() and Test::Test(const
std::stringstream& logStream) is identical up to saving the parameter
passed in rsi/esi in stack (in debug version). When I change the
constructor parameter to for example int, the crash occurs as well. So I
would conclude, the crash is caused by dereferencing destroyed local
variable. It just does not show itself in case of no parameter because the
stack memory conflict occasionally does not happen under these
circumstances and valgrind is not able to detect it properly.

Regards, Slava


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