C++ Memory Release Debug Assertion

Asked 1 years ago, Updated 1 years ago, 120 views

#include <iostream>
#include <fstream>
//http://is03.tistory.com/3 HOW to use eof

using namespace std;
#define FILENAME "data.txt"
ifstream fin;

class Matrix {

private:
    int size;
    int numOfNonZero;
    int* row;
    int* col;
    int* value;

public:
    Matrix(); // constructor
    Matrix(const Matrix&); // copy constructor
    ~Matrix();
    void display();
    void getData();
    Matrix operator*(const Matrix&);
};
Matrix::Matrix() {

}
void Matrix::getData() {

    fin >> this->size;
    fin >> this->numOfNonZero;
    row = new int[numOfNonZero];
    col = new int[numOfNonZero];
    value = new int[numOfNonZero];

    for (int i = 0; i < numOfNonZero; i++) {
        fin >> row[i] >> col[i] >> value[i];
    }
}
Matrix Matrix::operator*(const Matrix& rhs) {
    Matrix ans;
    return ans;
}
void Matrix::display() {
    cout << "The Matrix is : " << endl;
    for (int i = 0; i < this->numOfNonZero; i++) {
        cout << row[i] << " " << col[i] << " " << value[i] << endl;
    }

}

Matrix::~Matrix() {
    if (this->row != NULL) {
        delete[] this->row;
        delete[] this->col;
        delete[] this->value;
    }
}

Matrix::Matrix(const Matrix& rhs) {
    this->size = rhs.size;
    this->numOfNonZero = rhs.numOfNonZero;

    this->row = new int(numOfNonZero);
    this->col = new int(numOfNonZero);
    this->value = new int(numOfNonZero);

    for (int i = 0; i < numOfNonZero; i++) {
        this->row[i] = rhs.row[i];
        this->col[i] = rhs.col[i];
        this->value[i] = rhs.value[i];
    }

}

int main() {
    fin.open(FILENAME);

    Matrix A, B, C;
    A.getData();
    B.getData();
    A.display(); B.display();

    C = A * B;

    return 0;
}

Debug Assertion occurs when running in Visual Studio. The detailed error name is _CrtIsValidHeapPointer(block). When I looked at where the error occurred while debugging, it occurred in the deletion of the extinction. I don't understand why this error is occurring.

ps. From the main,
Not Matrix C; C = A * B; Matrix C = A * B;

If I do this, there will be no error again.;;

I'd appreciate it if you could give me an explanation point.

c++ assertion delete

2022-09-22 20:56

1 Answers

First of all, the two codes written in ps are not the same code.

Matrix C; // The default constructor is called as the object is created
C = A * B; // copy substitution operator called

Matrix C = A * B; // Copy constructor called as object is created

The copy substitution operator is called when you place the value of another object on an already created object Copy constructor is literally a constructor, so if there is a factor that is substituted at the time the object is created, it is called instead of the default constructor.

A copy substitution operator, like a copy constructor, is a function that the compiler automatically generates unless you define it directly.

Now let's move on to the question.

Why does this error occur?

Looking at the code, you defined the copy constructor yourself, and you are using the member variable of the object passed as a factor without a nullptr check.

for (int i = 0; i < numOfNonZero; i++) {
    This->row[i] = rhs.row[i]; // pointer variable is referenced.
    this->col[i] = rhs.col[i];
    this->value[i] = rhs.value[i];
}

Looking at the code that the copy constructor invokes, it passes the object returned through A * B as a factor.

C = A * B; // When the copy constructor of C is called

Operation A * B shows operator overloading as shown below. (This part seems to be not yet implemented.)

Matrix Matrix::operator*(const Matrix& rhs) {
    Matrix ans;
    return an; // just create a new object and return it...
}

The member pointer variables (row, etc.) in the Matrix class are not assigned values in the default constructor, but are assigned values through the getData() function.

Therefore, the resulting instance passed as a factor to the copy constructor of C has unassigned pointer variables, and an error occurred because it tried to use them.

And _CrtIsValidHeapPointer(block) I wonder what kind of error this error represents.

Occurs when referring to an invalid pointer. IsValidHeapPointer is literally checking that the pointer is valid.

Here, the Heap refers to the local heap memory of the program that is currently running, that is, to check whether the memory address that the pointer refers to is the address of the space allocated on the program, such as new.


2022-09-22 20:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.