0
Mat::Mat(int R, int C)
{
    Ar = new int[C*R];
    Co = C;
    Ro = R;
}

Mat::~Mat()
{
    delete[] Ar;
}

Yet i get "signal SIGABRT" on this "delete".

EDIT:
I use XCODE, also I tried to add a hard copy constructor. I still get this error in delete[]:

Mat::Mat(int R, int C)
{
    Ar = new int[C*R];
    Co = C;
    Ro = R;
}

Mat::Mat(const Mat& M): Co(M.Co), Ro(M.Ro)
{
    Ar = new int[M.Co*M.Ro];
    for (int i = 0; i<(M.Co*M.Ro); i++) {
        Ar[i] = M.Ar[i];
    }
}

Mat::~Mat()
{
    delete[] Ar;
}
  • Are you creating a `Mat` object anywhere without using your custom constructor? – Some programmer dude Jun 07 '12 at 09:13
  • I'm pretty sure this can be solved by using std::vector instead of int*. – stefaanv Jun 07 '12 at 09:16
  • Let me guess, you haven't implemented a proper copy constructor and assigment operator? – Christian Rau Jun 07 '12 at 09:24
  • 1
    The copy constructor you have here will have you deleting a single array multiple times. This is Not Good. – Hugh Jun 07 '12 at 09:24
  • A small explanation on Huw's comment: your copy constructor copies the pointer, not the actual array, so after the copy, 2 instances point to the same array and try to delete it. Your copy constructor does nothing more than the implicitly generated copy constructor, but it should make a new array and copy each entry in that array. (see also the rule of 3: you should also provide an assignment operator) – stefaanv Jun 07 '12 at 09:29

2 Answers2

6

This is more of psychic debugging but I am pretty much convinced, it is the root cause:
You should follow the Rule of Three.
You should provide your own copy constructor and copy assignment operator for class Mat which make deep copy of the pointer.

If you don't have custom versions for above mentioned functions then dynamic memory allocated to the pointer member Ar gets deallocated whenever a temporary copy of your class object is created and destroyed, eventually you are left with a dangling pointer member and finally delete [] is called on it resulting an Undefined Behavior and a crash.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • @MichaelMirkin `Ar(M.Ar)` And exactly this is the problem. It just copies the pointer, but it should allocate new storage for `Ar` and copy the actualy data. It isn't about just implementing a trivial copy constructor, the compiler can (and will) do that, too. You of course have to implement it properly. If you don't know how, then consult some introductory material on pointers and dynamic memory allocation. – Christian Rau Jun 07 '12 at 09:25
  • @MichaelMirkin: Indeed the problem is that your copy constructor makes a **Shallow copy** (*just points point member of new object being constructed to memory allocated to pointer member of object through which it is constructed*).It should make a **Deep Copy**, it should allocate memory to the pointer member of the object being constructed and then copy the contents of memory pointed by pointer member of object passed in as input argument to cctor. – Alok Save Jun 07 '12 at 09:26
  • Thank you my friend for your help, but still i tried it your way, by hard copy, and i get this error again. – Michael Mirkin Jun 07 '12 at 09:37
  • @MichaelMirkin You also have to do it in the assignment operator, of course. – Christian Rau Jun 07 '12 at 09:40
1

If anywhere in your code you have something like that:

{
Mat A(R1,C1);//create A.Ar
MAT B(A);//now B.Ar will point to A.Ar array
}//delete will be called twice for the same array
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94