title: Why is vector deleting destructor being called as a result of a scalar delete?
body: <p>I have some code that is crashing in a large system.
  However, the code essentially boils down to the following pseudo-code.
  I've removed much of the detail, as I have tried to boil this down to the bare bones;
  I don't think this misses anything crucial though.</p>
  
  <pre><code>// in a DLL:
  
  #ifdef _DLL
  #define DLLEXP __declspec(dllexport)
  #else
  #define DLLEXP __declspec(dllimport)
  #endif
  
  class DLLEXP MyClass // base class; virtual
  {
  public:
    MyClass() {};
    virtual ~MyClass() {};
  
    some_method () = 0; // pure virtual
  
    // no member data
  };
  
  class DLLEXP MyClassImp : public MyClass
  {
  public:
    MyClassImp( some_parameters )
    { 
      // some assignments...
    }
  
    virtual ~MyClassImp() {};
  
  private:
    // some member data...
  };
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>// in the EXE:
  
  MyClassImp* myObj = new MyClassImp ( some_arguments ); // scalar new
  // ... and literally next (as part of my cutting-down)...
  delete myObj; // scalar delete
  </code></pre>
  
  <p>Note that matching scalar new and scalar delete are being used.</p>
  
  <p>In a Debug build in Visual Studio (2008 Pro),
  in Microsoft's &lt;dbgheap.c>,
  the following assertion fails:</p>
  
  <pre><code>_ASSERTE(_CrtIsValidHeapPointer(pUserData));
  </code></pre>
  
  <p>Near the top of the stack are the following items:</p>
  
  <pre><code>mydll_d.dll!operator delete()
  mydll_d.dll!MyClassImp::`vector deleting destructor'()
  </code></pre>
  
  <p>I think this ought to be</p>
  
  <pre><code>mydll_d.dll!MyClassImp::`scalar deleting destructor'()
  </code></pre>
  
  <p>That is, the program is behaving as if I'd written</p>
  
  <pre><code>MyClassImp* myObj = new MyClassImp ( some_arguments );
  delete[] newObj; // array delete
  </code></pre>
  
  <p>The address in <code>pUserData</code> is that of <code>myObj</code> itself (as opposed to a member).
  The memory around that address looks like this:</p>
  
  <pre><code>                                ... FD FD FD FD
  (address here)
  VV VV VV VV MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
  FD FD FD FD AB AB AB AB AB AB AB AB EE FE EE FE
  ...
  </code></pre>
  
  <p>where the four <code>VV</code>s are presumably the address of the virtual function table,
  the <code>MM...MM</code> is recognisable member data,
  and the other bytes are various special markers put in place by the debugger
  (e.g. the <code>FD FD</code>s are 'guard bytes' around the object's storage).</p>
  
  <p>Shortly before the assertion failure I do see the <code>VV</code>s change,
  and wonder if that is due to a switch to the base class's virtual function table.</p>
  
  <p>I'm aware of the problem of the wrong level in the class hierarchy undergoing destruction.
  That's not the problem here; my destructors are all virtual.</p>
  
  <p>I note Microsoft's page
  "BUG: Wrong Operator Delete Called for Exported Class"
  <a href="http://support.microsoft.com/kb/122675" rel="noreferrer">http://support.microsoft.com/kb/122675</a>
  but that seems to be regarding the wrong executable (with the wrong heap) attempting to take responsibility for destruction of the data.</p>
  
  <p>In my case, it's that the wrong 'flavour' of deleting destructor appears to be being applied:
  i.e. vector rather than scalar.</p>
  
  <p>I am in the process of trying to produce minimal cut-down code that still exhibits the problem.</p>
  
  <p>However, any hints or tips to help with how to investigate this problem further would be much appreciated.</p>
  
  <p>Perhaps the biggest clue here is the <code>mydll_d.dll!operator delete()</code> on the stack.
  Should I expect this to be <code>myexe_d.exe!operator delete()</code>,
  indicating that the <code>DLLEXP</code>s have been 'lost'?</p>
  
  <p>I suppose this could be an instance of a double-delete (but I don't think so).</p>
  
  <p>Is there a good reference I can read regarding what <code>_CrtIsValidHeapPointer</code> checks for?</p>