Alright so I have a class of Matrix which looks like
Code:
class Matrix
{
private:
string _name;
int errorInput;
int _row;
int _col;
double ** _mat;
public:
//Constructors
Matrix();
Matrix(const Matrix &m);
//Deconstructor
~Matrix();
//swap for copy
void swap(Matrix& m);
//get functions for variables
string getName(void);
int getRow(void);
int getCol(void);
int getError(void);
//set functions for variables
void setName(string name);
//clear the matrix
void clear(void);
//transpose Matrix
Matrix transpose(void);
//operators to overload
Matrix & Matrix::operator = (const Matrix & mMatrix);
friend std::istream& operator>>(std::istream &is, Matrix & mMatrix);
friend ostream& operator<<(ostream& os, const Matrix& dt);
const Matrix operator+(double adder) const;
const Matrix operator-(double subber) const;
const Matrix operator*(double multer) const;
const Matrix operator+(const Matrix &rhs) const;
const Matrix operator-(const Matrix &rhs) const;
const Matrix operator*(const Matrix &rhs) const;
And whenever an instances destructor gets called I face heap errors such as
In non-debugging mode i get this
Code:
HEAP CORRUPTION DETECTED: after normal block (#184) at 0x004E95A8.
CRT Detected that the application wrote to memory after the end of heap buffer
This in debug mode. Says windows has triggered a break point and brings me to the function below.
Code:
/***
*int _CrtIsValidHeapPointer() - verify pointer is from 'local' heap
*
*Purpose:
* Verify pointer is not only a valid pointer but also that it is from
* the 'local' heap. Pointers from another copy of the C runtime (even in the
* same process) will be caught.
*
*Entry:
* const void * pUserData - pointer of interest
*
*Return:
* TRUE - if valid and from local heap
* FALSE otherwise
*
*******************************************************************************/
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;
if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;
return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}.
Now I have done as much investigating possible, in the matter of the address that gets deleted.
Basically a flow chart of what happens is the following
Code:
I call the clear function of a temp instance a matrix.
-> then in the clear function I use a copy and swap type of clear where I create
a new temp instance of matrix and swap that with with the current matrix's contents
which then when leaves calls the destructor of the temp instance which now holds
the old data.
-> in the destructor i go row by row and delete the pointer of each row and
then delete the head pointer. When i try to delete a row i get the errors.
My destructor looks like
Code:
Matrix::~Matrix()
{
for(int i = 0; i < _row; i++)
{
delete[] _mat[i];
}
delete[] _mat;
_mat = NULL;
}
And my copy constructor looks like
Code:
Matrix::Matrix(const Matrix &m)
{
int i,j;
_row = m._row;
_col = m._col;
_name = m._name;
errorInput = m.errorInput;
_mat = new double *[_row];
for(i=0;i<_row;i++)
{
_mat[i] = new double [_col];
for(j=0;j<_col;j++)
{
_mat[i][j] = m._mat[i][j];
}
}
}
I have my destructor print out addresses before deleting them and they all check out to the data I want to get deleted. But for some reason I get those two errors. Thoughts?