Возможно, у вас есть ошибка где-то еще в вашем источнике.Я попытался повторить ошибку, используя следующий код:
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
const char SEPARATOR = ':';
struct Foo
{
public:
int room;
int money;
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());
}
string 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;
}
};
int main()
{
Foo f;
f.doSomething("--234:12");
std::cout << f.room << " - " << f.money << std::endl;
}
Затем запустил valgrind:
valgrind --tool=memcheck <executable>
и вывел:
HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 2 allocs, 2 frees, 61 bytes allocated
All heap blocks were freed -- no leaks are possible
For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
Итак,возможно, ваша проблема не в этой части кода