I recently wrote code that I thought was fine, but a colleague says that it caused random crashes in our application. Offensive code is recorded in an undiscovered stream. My question is: should it normally write to a thread that was not open? This happened when the class initialization did not open its stream to register debugging information. But subsequent methods will still use the unopened stream. Here is an example:
class A {
public:
A(const std::string& fname) {
if (!fname.empty()) {
m_debug_log.open(fname.c_str());
}
}
void DoSomething() {
m_debug_log << "doing something useful now" << std::endl;
}
private:
std::ofstream m_debug_log;
};
My colleague says that he stopped random crashes by wrapping all output operations on m_debug_logwith validation on the thread. Thus, output operations are only performed when it m_debug_logis a valid output stream.
void DoSomething() {
if (m_debug_log)
m_debug_log << "doing something useful now" << std::endl;
}
, , , . , . (, , .)
, . , undefined . ? , . VS 2010, VS 2013, Ubuntu 12.04 Centos 6.3, . , .