I'm rather perplexed on the cause of this bug and I just had a co-worker come over and he couldn't find anything wrong with it either. Its really simple. I have a class that has an internal std::vector<std::string> member, and two public functions that allow you to push and pop strings from that vector. Here's the relevant snippets from the code:
// Header file
class LogManager
{
public:
/** \brief Adds a function name to the call stack, intended to be called upon function entry
*** \param name The name of the function being entered
**/
void LogFunctionEntry(std::string name);
//! \brief Removes the most recent function entry that was logged
void LogFunctionExit();
private:
//! \brief Maintains a call stack of function names, used for debugging purposes on error
std::vector<std::string> _call_stack;
};
// Source file
void tLogManager::LogFunctionEntry(string name)
{
_call_stack.push_back(name);
}
void tLogManager::LogFunctionExit()
{
if (_call_stack.empty() == true)
{
Warning(__FILE__, __LINE__, "attempted to log a function exit when no function entries "
"were on the call stack\n");
}
else
{
_call_stack.pop_back();
}
}
// Use case
void foo
{
Log->LogFunctionEntry("entered foo");
......
Log->LogFunctionExit();
}
Looks simple enough, doesn't it? The application that uses this code is a test suite that executes tests multiple times in a loop. Sometimes on a random iteration through that loop, the application will crash after calling LogFunctionExit(). The warning message in that function is never printed in the crash case (nor in the non-crash case). Somehow, popping a string from the back of a non-empty vector induces a crash on Windows. I ran windbg on the app and it didn't give me much useful information. In the call stack I see it in the LogFunctionExit function and then further down its in some STL code.
What's even odder is that the crash occurs on the exact same call to LogFunctionExit() each time. There's no dubious code around the location of the crash at all, so I don't see how that could be at fault. I'm scratching my head at this point trying to figure out exactly what is causing this random crash, hence why I'm posting here to see if anyone else has some insight.
My questions are:
1) Do you see anything wrong with the above code and/or usage example that could be causing this crash?
2) On a related note, would it be better if my argument to LogFunctionEntry was a const string reference instead of just a string pass-by-value?
Thanks in advance