Valgrind抱怨substr調用。Valgrind錯誤的原因是什麼?
string Message::nextField(string& input) {
int posSeparator = input.find_first_of(SEPARATOR);
string temp;
temp = input.substr(0, posSeparator); //Error points to this line
input.erase(0, posSeparator + 1);
return temp;
}
錯誤雲:在12個塊
290個字節中記錄損失11
什麼該函數基本上是解析輸入,返回由分隔符隔開的字符串的部分絕對丟失。這個功能是從另一個類的方法調用,並且有一個定義:
void doSomething(string input) {
input.erase(0,2);
string temp = nextField(input);
this->room = atoi(temp.c_str());
temp = input;
this->money = atoi(temp.c_str());
}
沒有別的怪異或足夠重要,包括在這裏。 我從Eclipse Indigo的Valgrind分析中使用Valgrind的默認設置。 任何想法?
你在編譯期間是否優化?如果是這樣,不要。這導致了valgrind的大量虛假報告。 –
你可能會寫'string temp = input.substr(0,posSeparator);'來初始化字符串而不是分配給它。但是,與您的問題無關,這並不明顯。 –
@DavidHammen不,我不是。 – Erandros