1

I've got a destructor in my frame class that does:

delete this->frameMatrix; 

Where framematrix is of class Matrix with this as constructor and destructor:

// Constructor: Initialize matrix & sizes
Matrix::Matrix(int width, int height)
{
        table = new double* [height];
        for(int i = 0; i < height; i++)
                table[i] = new double [width];

        // Set all values to zero
        for(int row = 0; row < height; row++)
        {
                for(int col = 0; col < width; col++)
                {
                        table[row][col] = 0;
                }
        }

        this->width = width;
        this->height = height;
}

// Destructor: delete matrix
Matrix::~Matrix()
{
        for(int row = 0; row < height; row++)
                delete [] table[row];
        delete [] table;

        this->width = 0;
        this->height = 0;
}

When calling the delete on frameMatrix the program gives an assertion failed in the destructor of matrix.

I'm I doing something wrong because I don't see the problem on how I delete the 2d double array.

EDIT:

Copy constructor:

Matrix::Matrix(const Matrix &m)
{
    this->height = m.getHeight();
    this->width = m.getWidth();

    this->table = new double* [height];
        for(int i = 0; i < height; i++)
                this->table[i] = new double [width];

        for(int row = 0; row < height; row++)
        {
                for(int col = 0; col < width; col++)
                {
                    this->table[row][col] = m.table[row][col];
                }
        }

}

My overloading =

    Matrix &operator = (const Matrix &m)
    {
        this->height = m.getHeight();
        this->width = m.getWidth();

        this->table = new double* [height];
        for(int i = 0; i < height; i++)
            this->table[i] = new double [width];

        for(int row = 0; row < height; row++)
        {
            for(int col = 0; col < width; col++)
            {
                this->table[row][col] = m.table[row][col];
            }
        }
    }
user1007522
  • 7,858
  • 17
  • 69
  • 113
  • It's won't be causing an assertion failure, but your `operator=` has a memory leak. You never `delete[]` the memory it currently owns before assigning it new memory. You also don't return anything from it (you should be returning `*this`). I recommend reading http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three and http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom – Yuushi Nov 28 '12 at 00:21

1 Answers1

2

Do you have a copy constructor and operator=? You need to override the default implementations of these methods since you've got dynamically allocated pointers.

class Matrix
{
public:
    Matrix(const Matrix &);
    Matrix &operator = (const Matrix &);
};

Without them whenever a Matrix object is copied the new object will have the same pointers as the original object. The destructor will end up double-deleteing the arrays.

On a side note, there's no need to reset width and height in the destructor. These fields are inaccessible after the object is destroyed.

this->width = 0;
this->height = 0;


Boilerplate code for the assignment operator:

Matrix &operator = (const Matrix &m)
{
    // Don't do anything for `m = m;`.
    if (&m == this)
        return *this;

    // Delete existing contents.
    ...

    // Copy other matrix.
    ...

    return *this;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Thanks. I've wrote a copy constructor is that enough or do I still need a = overloading? Because the = and copyconstructor does the same? In edit my copy constructor. – user1007522 Nov 28 '12 at 00:12
  • @user1007522 You need both. They're similar but different. – John Kugelman Nov 28 '12 at 00:16
  • I've added the other one too, thats the same as my copy. Still got the assertion bug:s – user1007522 Nov 28 '12 at 00:18
  • @user1007522 See my edit for a boilerplate assignment operator. It shouldn't be the same as your copy constructor. It's more like a destructor followed by copy constructor. And the `if` at top is easy to forget but important. – John Kugelman Nov 28 '12 at 00:27