这个Valgrind错误的原因是什么?

era*_*ros 6 c++ string valgrind

Valgrind抱怨一个substr调用.

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;
}
Run Code Online (Sandbox Code Playgroud)

错误:
12个块中的290个字节肯定在丢失记录1中丢失1
该函数的作用基本上是解析输入,返回由SEPARATOR字符分隔的字符串部分.使用下一个定义从另一个类的方法调用此函数:

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());
}
Run Code Online (Sandbox Code Playgroud)

没有别的奇怪或重要的东西可以包含在这里.我使用Eclipse Indigo的Valgrind分析中的Valgrind的默认设置.有任何想法吗?

thi*_*ton 0

您不检查 posSeparator 是否实际上与 string::npos 不同 - 这可能会导致擦除问题。这是一个疯狂的尝试,但无论如何它可能会修复一个错误。