Valgrind errors

From ODF::Wiki

Jump to: navigation, search

Since many of our programmers use valgrind, here is a list of some error outputs and what they mean.

Note: This lessons is for advanced users. If you are new to valgrind, we suggest you to read the valgrind howto article, first.

[edit] Uninitialized Values

  • Use of uninitialised value of size 4
  • Conditional jump or move depends on uninitialised value(s)

This means that you are reading a variable that you did not write a value to.

The easiest case is:

void main(void) {
  int x;
  printf("x is %d\n",x); // What is x? What did we set it to?
}

In this case, it won't cause a segfault, but simply apply x a random number. However, if you do that with pointers, the pointer will access a random sequence in memory, which causes a segfault almost every time!

Here is another dangerous example:

class Base {
  std::string name;
  int initialize_name() {
    name = "idiot";
  }
};
 
class Sub : Base {
  std::string name;
  // oops! on inheriting, the user forgot to erase one of the name variables
  // this means we have two equal named variables now!
  int read_name() {
    puts(name.c_str());
  }
};
 
void main(void) {
  Sub s;
  s.initialize_name(); // initializes Base::name
  s.read_name(); // reads Sub::name -> SEGFAULT
}

To cut a long story short, some reasons for forgetting to initialize values are:

  • initialization simply forgotten
  • inheritance (see example above)
  • ... (add more if you find more!)

[edit] Giving Memory free

  • Mismatched free() / delete / delete []

This means that you, for example, used new[] to create an Array, but deleted it with a simple delete. The following code creates that error:

int length;
// ...
char* data = new char[length];
//...
delete data;
Personal tools