Valgrind是給我一個無效的讀取錯誤:爲什麼Valgrind會給出「無效的大小爲1的讀取」錯誤?
==37561== Invalid read of size 1
==37561== at 0x7E81: strlen (vg_replace_strmem.c:427)
一類中的以下代碼(我認爲可能與尾\0
,但我不知道)。
std::queue<std::string> errorLog; ///< FIFO used to store errors
const char *Monitor::popErrorFromErrorLog() {
if (!errorLog.empty()) {
std::string str = errorLog.front();
errorLog.pop();
return str.c_str();
} else {
return nullptr;
}
}
void Monitor::reportError(std::string s) {
std::ostringstream err;
err << "Error reported: " << s << std::endl;
errorLog.push(err.str());
}
任何想法這裏有什麼問題嗎?
謝謝 - 需要一個返回指針的方法(如果沒有錯誤,它將爲null),但是已經修改了一個std :: string並檢查empty()。 – John