I have an assignment to write a class that dynamically allocates a 2D array, then I have to write functions to work with it. One of the functions is an overload function of operator+. Everything works as expected, even the operator+ functions until the program calls the destructor. The destructor has a simple job of releasing the memory allocated previously. I know the code is correct and I have tested it manually, but when the computer calls this function at the end of the program, I get "Unhandled exception at 0x000566E1 in 2DArray.exe: 0xC0000005: Access violation reading location 0xFEEEFEF2." I tried all sorts of things, I have been sitting at this problem for two days now and I'm exhausted. Its due this Friday. If anyone can try and help, I would appreciate it.
Here are the two functions that I think the problem is buried in. Also, note that when there is no destructor, the program runs completely fine, but I have to make sure the memory gets released before the program quits, its part of the assignment.
Square_Matrix Square_Matrix::operator+(Square_Matrix & object)
{
Square_Matrix sum;
sum.Set_Size(size);
for (int i = 0; i < size; i++)
{
for (int b = 0; b < size; b++)
{
sum.Set_Elem((matrix[i][b] + object.Get_Elem(i, b)),i, b);
}
}
return sum;
}
Square_Matrix::~Square_Matrix()
{
// destructor
// release the allocated memory
if (size > 0)
{
for (int d = 0; d < size; d++)
{
delete[] matrix[d];
}
delete[] matrix;
size = 0;
}
}