Question

say I created a custom Array class and have following constructor:

    Array::Array(size_t size)
    {
      mySize = size;
      //myData is of type int*
      myData = new int[mySize]; // this stuff is allocated on a heap
    }

To my knowledge, a default copy constructor in this case would be implemented like that:

    Array::Array(Array& a)
    {
      mySize = a.mySize;
      myData = a.myData; //points to the same memory on the heap
    }

Finally, say I have following code in my main.cpp file

     int main()
     {
       Array a(1);
       Array b(a); //copy constructor is invoked
     }

What I expected to be was a memory leak due to deletion of myData pointers to the free store memory, BUT I have following runtime error :

*** glibc detected *** ./main.out: double free or corruption (fasttop): 0x086ad008 ***

WHY? It seems that ~Array() somehow auto frees memory allocated on a heap - but this is very counter intuitive to me. Maybe I am missing something?

UPDATE:

      class Array{
         private:
           size_t mySize;
           int *myData;
           ...

UPDATE 2:

main.cpp:

   #include <iostream>
   #include "array.h"

   int main()
   {
     Array a(1);
     Array b(a);
   }

array.h:

   #ifndef ARRAY_H_
   #define ARRAY_H_
   #include <stddef.h>
   class Array{
   private:
       size_t mySize;
       int *myData;
   public:
       Array(size_t size);
       ~Array();
       void set(int i,int val);
       int get(int i);
       size_t getSize();
   };
   #endif

array.cpp:

   #include "array.h"

   Array::Array(size_t size)
   {
     mySize = size;
     myData = new int[mySize];
   }

   Array::~Array()
   {
     delete[] myData;
   }

   void Array::set(int i, int val)
   {
     if(i>=0 && i<mySize)
       myData[i] = val;
   }

   int Array::get(int i)
   {
     if(i>=0 && i<mySize)
       return myData[i];
     else return -1;
   }
   size_t Array::getSize()
   {
     return mySize;
   }
Was it helpful?

Solution

I think in your destructor you have

 Array::~Array(void)
 {
      delete [] myData; //points to the same memory on the heap
 }

The problem is double free

Example:

int main()
{
       Array a(1);  // here a.myData = 0x12345678
       Array b(a); //copy constructor is invoked // here b.myData = 0x12345678

       // pop  stack (call destroy of object)
       // delete b.myData
       // delete a.myData already delete
}

Double free

EDIT: For your copy contructor use const because you don't modify a.

 Array::Array(const Array& a)
 {
      mySize = a.mySize;
      myData = a.myData; //points to the same memory on the heap
 }

Good Luck !

OTHER TIPS

So... despite your claims, it turns out you do delete the array in the destructor, and the copy constructor is a shallow copy, so you get a double delete. Simple.

Array::~Array()
{
  delete[] myData;
}

Since this is a dynamic array, it should own the data, therefore you are right to delete in the destructor, but you need to "deep" copy in the copy constructor and the assignment operator. See the rule of three.

Your copy is a shallow copy, so IF you have a destructor that frees memory, each object attempts to delete the same memory.

You don't have to do a shallow copy. You could write your own explicit copy constructor that copies the actual data into a newly allocated array.

I like Google's suggestion to disable copy and assignment constructors and prefer explicit CopyFrom() methods.

"double free" means that the same pointer has been given to delete[] twice. because in your destructor (presumably) it's delete[]-ed both in object a and b. practical solution: use std::vector, don’t mess with raw arrays etc. needlessly.

I think your desctructors (from array 'a' and 'b') are trying to free the same memory (double free). Maybe you should check you myData before freeing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top