You probably have an error somewhere else in your source. I tried to replicate the error using the following code:
#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; }
Then run valgrind:
valgrind --tool=memcheck <executable>
and output:
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
So maybe your problem is not in this part of the code
source share