title: Valgrind reporting Mismatched free() / delete / delete []
body: <p>I'm writing a library that will operate on multiple systems (some of which do not have <code>malloc</code> or a stdlib). In my stdlib (different lib), I am overriding the <code>new</code> and <code>delete</code> operators to make generic calls to functions (this example doesn't have these functions). Each system will override these generic calls to their respective memory allocation devices.</p>
  
  <p>The issue is when I attempt to do this. Here is some stripped down example code to reproduce the issue:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  
  void* operator new(unsigned long size) {
          return std::malloc(size); // would normally call an intermediate function which would be overridden by the system
  }
  
  void operator delete(void* object) {
          std::free(object); // would normally call an intermediate function which would be overridden by the system
  }
  void operator delete(void* object, unsigned long size) {
          std::free(object); // would normally call an intermediate function which would be overridden by the system
  }
  
  class MyClass {
  
  };
  
  int main() {
      MyClass* myClass = new MyClass();
      delete myClass;
  }
  </code></pre>
  
  <p>When I build it with plain <code>gcc-6</code> (no args) and run with <code>valgrind</code> (no args), I get this error:</p>
  
  <pre><code>==11219== Mismatched free() / delete / delete []
  ==11219==    at 0x4C2DD6B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==11219==    by 0x108730: operator delete(void*, unsigned long) (in /home/chris13524/tmp/test.o)
  ==11219==    by 0x10875A: main (in /home/chris13524/tmp/test.o)
  ==11219==  Address 0x5200040 is 0 bytes inside a block of size 1 alloc'd
  ==11219==    at 0x4C2D1AF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==11219==    by 0x108745: main (in /home/chris13524/tmp/test.o)
  </code></pre>
  
  <p>It appears the <code>delete</code> operator is working correctly, but Valgrind is overriding my overridden <code>new</code> operator. Any idea how to fix this?</p>
  
  <p>Removing the intermediate functions is not an option as I have other code in there.</p>
  
  <p>Example of how it works on my real program (again, not shown in my example):</p>
  
  <pre><code>new =&gt; create =&gt; &lt;intermediate code&gt; =&gt; createImpl =&gt; malloc
  create =&gt; &lt;intermediate code&gt; =&gt; createImpl =&gt; malloc
  </code></pre>
  
  <p>I'm using gcc v6.2.0, valgrind v3.12.0, and Ubuntu 16.10.</p>