Fork me on Github
Fork me on Github

Joe Dog Software

Proudly serving the Internets since 1999

up arrow Valgrind: Apology Edition

The things Your JoeDog will do for you….

A couple weeks ago he complained about Valgrind (and probably a whole bunch of other stuff but we’re talking about Valgrind now). His snippet was leaking memory and valgrind was unable to identify the leak. The leak was manually identified in this function:

ARRAY
array_destroy(ARRAY this) 
{
  int i;

  for (i = 0; i < this->length; i++) {
    xfree(this->data[i]);  
  } 
  xfree(this->data);
  this = NULL;
  return this; 
}

While we freed the elements of the array, we never freed the array itself. The leak is fixed like this:

ARRAY
array_destroy(ARRAY this) 
{
  int i;

  for (i = 0; i < this->length; i++) {
    xfree(this->data[i]);  
  } 
  xfree(this->data);
  xfree(this);
  this = NULL;
  return this; 
}

Today Your JoeDog was coding on the train again.  Before he boarded, he downloaded the above code onto his snazzy Linux laptop.  He was turning caffeine into code when wouldn’t you know it? Another stinkin’ memory leak. “What the hell,” he thought. “Let’s give valgrind another try.” Unfortunately it wasn’t installed on snazzy Linux laptop. For some reason, Amtrak’s proxy won’t allow downloads larger than 10MB. Stupid Amtrak.

That’s easy to bypass. Your JoeDog established an ssh tunnel from his laptop to this webserver and proxied to the Ubuntu repository.

 

--2014-10-25 10:15:13--http://us.archive.ubuntu.com/ubuntu/pool/main/v/valgrind/valgrind_3.10~20140411-0ubuntu1_amd64.deb
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:11111... connected.
Proxy request sent, awaiting response... 200 OK
Length: 15078790 (14M) [application/x-debian-package]
Saving to: ‘valgrind_3.10~20140411-0ubuntu1_amd64.deb’
100%[=============================================>] 15,078,790 290KB/s in 73s
2014-10-25 10:16:26 (202 KB/s) - ‘valgrind_3.10~20140411-0ubuntu1_amd64.deb’ saved [15078790/15078790]
 Funny thing. That copy of valgrind found the leak. Guess where it was? Give up?
ARRAY
array_destroy(ARRAY this) 
{
  int i;

  for (i = 0; i < this->length; i++) {
    xfree(this->data[i]);  
  } 
  xfree(this->data);
  //xfree(this);
  this = NULL;
  return this; 
}

In order to illustrate his first post on valgrind, Your JoeDog commented out the fix he told you about. Well, this second copy of valgrind found it. So what gives?

1. Valgrind works and I’m sorry I busted on it.

2. RedHat’s version doesn’t seem to work but Ubuntu’s does.