2

Running Valgrind against an existing codebase, I am getting a lot of "Mismatched free / delete/ delete[]" errors. Many of them are a repeat of one problem: it claims that at line XXX a delete operation is being used, whereas at line YYY a malloc operation is used. However, when I open the file that it complains about and navigate to the line numbers indicated, I find that the memory was not allocated with malloc but with new. The allocated object was an standard ifstream and neither new[] nor delete[] are being used.

I'm running Valgrind 3.5. Does anyone have any idea what is happening? I cannot see how this can be a real error, but I've seen some people claim that Valgrind doesn't turn up many false positives, so I want to have some confidence that this is fake before suppressing it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
  • I hope this is not a duplicate question. I have searched through a good number of the Valgrind questions and have not found any satisfactory answers. By the way, I cannot post the code. – Keith Pinson Jul 09 '12 at 20:39
  • 5
    @Karzak - you might not be able to post the complete real code, but nothing is stopping you from creating a small, complete example that reproduces the problem. – Flexo Jul 09 '12 at 20:41
  • 1
    FWIW I've never seen valgrind be wrong about mismatched free/delete/delete[] before. – Flexo Jul 09 '12 at 20:42
  • surely you can at least post the lines where the object is constructed and destructed? Or would you need to kill us then... ;-) – Roddy Jul 09 '12 at 21:25
  • Can you paste the precise error message? – Robᵩ Jul 09 '12 at 21:26
  • @Robᵩ I know this makes it tough, but I am not free to post that. Sorry. – Keith Pinson Jul 09 '12 at 21:34

1 Answers1

3

You don't provide a sample program, so this is a crystal-ball guess.

Your program provides an operator new but is missing an operator delete. The following sample program produces the same error message you are seeing:

#include <new>
#include <cstdlib>

/*
 * Sample program that provides `operator new`, but not `operator delete`.
 */

// minimal version of new for demonstration purpose only
void* operator new(size_t numBytes) {
  return malloc(numBytes);
}

int main () {
  int *p = new int;
  delete p;
}
Jeff Widman
  • 22,014
  • 12
  • 72
  • 88
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Is there a difference between `delete` and `operator delete(...)`? Strictly speaking, Valgrind is giving me an error about the second one. – Keith Pinson Jul 09 '12 at 21:22
  • Yes, there is, but the distinction is moot for this discussion. Run my program -- if it produces the same error message as your program does, then you have a clue. Read the source to your program, checking for the definition of `operator new` without a matching `operator delete` definition. – Robᵩ Jul 09 '12 at 21:26