Guido Tarsia
Guido Tarsia

Reputation: 2172

What is the reason for this Valgrind error?

Valgrind is complaining with a substr invocation.

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;
}

The error goes:
290 bytes in 12 blocks are definitely lost in loss record 1 of 1
What the function does is basically parse the input, returning portions of string separated by SEPARATOR character. This function is invoked from another class's method with the next definition:

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());
}

There's nothing else weird or important enough to be included here. I use the default setup for Valgrind from Eclipse Indigo's Valgrind profiling. Any ideas?

Upvotes: 6

Views: 1052

Answers (3)

thiton
thiton

Reputation: 36059

You don't check if the posSeparator is actually different from string::npos - this might cause problems in the erase. It's a wild shot, but it might fix a bug anyway.

Upvotes: 0

ks1322
ks1322

Reputation: 35775

It is probably not a bug in your code. This error could be reported due to details of implementation of C++ standard library. To verify this try the following from Valgrind FAQ:

With GCC 2.91, 2.95, 3.0 and 3.1, compile all source using the STL with -D__USE_MALLOC. Beware! This was removed from GCC starting with version 3.3.

With GCC 3.2.2 and later, you should export the environment variable GLIBCPP_FORCE_NEW before running your program.

With GCC 3.4 and later, that variable has changed name to GLIBCXX_FORCE_NEW.

Upvotes: 2

Marios V
Marios V

Reputation: 1174

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 a ran valgrind:

valgrind --tool=memcheck <executable>

and the output was:

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)

So, probably your problem is not in this part of code

Upvotes: 1

Related Questions