Thread: Dynamic Memory Problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97

    Dynamic Memory Problem

    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?
    Last edited by omGeeK; 04-15-2012 at 06:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory allocation problem
    By linuxlover in forum C Programming
    Replies: 17
    Last Post: 11-25-2010, 01:11 PM
  2. Dynamic Memory Problem - ADT
    By adamdavis in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2010, 03:51 PM
  3. dynamic memory allocation problem
    By firyace in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 09:57 PM
  4. Dynamic memory problem
    By *Tom* in forum C Programming
    Replies: 8
    Last Post: 09-11-2006, 10:50 AM
  5. Help with a problem regarding dynamic memory and arrays
    By Michty in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2006, 01:26 PM