
On Mon, 25 Nov 2013 13:21:33 +0100, Steven Maenhout
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