1

I have a problem in my C++ program after it has been running for a while I get a crash and the crash dump shows :

msvcr120.dll!free(void * pBlock) Line 51 C

~MyClass::`scalar deleting destructor'(unsigned int) C++

Here is some brief details of the member variable in question.

//Header file
BYTE* m_pBuffer;

//Constructor
m_pBuffer = new BYTE[256 * 1024];

//Destructor
if( m_pBuffer )
{
    delete[] m_pBuffer ;   <-- Crashes here
}

What exactly does the term scalar deleting destructor mean? and what could be causing it?

Harry Boy
  • 4,159
  • 17
  • 71
  • 122
  • 2
    My educated guess would be heap corruption, quite likely in seemingly unrelated part of code. Heap corruption bugs are nasty this way. – Igor Tandetnik Aug 03 '16 at 13:46
  • 1
    1) You don't need to check for a null pointer when issuing a `delete[]`. 2) Why not use `std::vector` instead of `BYTE *`? – PaulMcKenzie Aug 03 '16 at 13:51
  • whats the benefit of std::vector over BYTE* ? (Apart from having to manually call delete on the pointer) – Harry Boy Aug 03 '16 at 14:12
  • 1
    See this: http://stackoverflow.com/questions/3373193/why-is-vector-deleting-destructor-being-called-as-a-result-of-a-scalar-delete?rq=1 – crisron Aug 03 '16 at 14:24
  • 1
    @HarryBoy It does much more than not have you write `delete`. It makes it so that you don't have to write a destructor at all. In addition, usage of `std::vector` over raw pointers and hand-coded dynamic allocation alleviates you from having to write a user-defined copy constructor and assignment operator for your class. Does your current class have those two functions, or do you have these two functions "turned off"? If not, then your `MyClass` is not safely copyable, i.e. making copies of instances of those classes will lead to memory corruption. – PaulMcKenzie Aug 03 '16 at 14:25
  • No my class does not have these two functions. Are they needed even though my class is never assigned to another variable? – Harry Boy Aug 03 '16 at 14:28
  • 1
    @HarryBoy You cannot guarantee that no copying is going on (whether you do it explicitly, or whether the compiler does so) unless you turn off these functions. Either you need to make these functions `private` and unimplemented, or you use the new `C++11`'s `=delete` syntax for those functions. Read up on the [Rule of 3](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – PaulMcKenzie Aug 03 '16 at 14:37

0 Answers0