My destructor keeps throwing me exceptions and I dont know whats causing them. I think I did everything right, can somebody help me ? This is the code where I create a Matrix which is represented with a double pointer. Running the code gives me an exception when the destructor is called. My guess is that something is wrong with the move/copy constructor or with the initializing constructor which I copied from the web.
The error from Visual Studio is:
Exception thrown: read access violation.
this->**array** was 0x111011101110112.
Matrix.h
#pragma once
#include <iostream>
class Matrix {
size_t rows;
size_t cols;
int** array;
public:
~Matrix();
Matrix(size_t, size_t);
Matrix(const Matrix&);
Matrix(Matrix&&);
Matrix(size_t, size_t, std::initializer_list<std::initializer_list<int>>);
};
Matrix.cpp
#include "Matrix.h"
Matrix::~Matrix() {
for (size_t i = 0; i < rows; i++) {
delete[] array[i];
}
delete[] array;
}
Matrix::Matrix(size_t rows, size_t cols)
: rows(rows), cols(cols), array(new int* [rows]) {
for (size_t i = 0; i < rows; i++) {
array[i] = new int[cols];
}
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
array[i][j] = 0;
}
}
std::cout << "CONSTRUCTOR" << std::endl;
}
Matrix::Matrix(size_t rows, size_t cols, std::initializer_list<std::initializer_list<int>> list)
: rows(rows), cols(cols), array(new int* [rows]) {
for (size_t i = 0; i < rows; i++) {
array[i] = new int[cols];
}
size_t i = 0;
for (auto row : list) {
size_t j = 0;
for (auto elem : row) {
array[i][j] = elem;
j++;
}
i++;
}
}
Matrix::Matrix(const Matrix& other)
: rows(other.rows), cols(other.cols), array(new int* [other.rows]) {
if (other.array != nullptr) {
for (size_t i = 0; i < other.rows; i++) {
array[i] = new int[other.cols];
}
for (size_t i = 0; i < other.rows; i++) {
for (size_t j = 0; j < other.cols; j++) {
array[i][j] = other.array[i][j];
}
}
}
else {
delete[] array;
array = nullptr;
}
std::cout << "COPY" << std::endl;
}
Matrix::Matrix(Matrix&& other)
: rows(other.rows), cols(other.cols), array(other.array) {
other.array = nullptr;
std::cout << "MOVE" << std::endl;
}
main.cpp
#include "Matrix.h"
Matrix f(Matrix m) { return m; }
int main() {
Matrix m(2, 2, { {1,2},{2,3} });
Matrix k(f(m));
}
I tried making new projects numerous times with adding different stuff but I really don't know where is the problem.