dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Problem with Matrix template class

  1. #1
    Join Date
    Apr 2008
    Posts
    16

    Problem with Matrix template class

    Hi,

    I have built a template class for matrices:

    Code:
    template<class T>
    class CMatrix
    {
    public:	
        CMatrix(int cols=0, int rows=0);
        //
        virtual ~CMatrix();
        //    
    private:
        int m_Columns;
        int m_Rows;
        /**
         * Contain matrix data
         */	
        T** m_Data;
    };
    The implementation of CMatrix<T>'s constructor and destructor as follows:

    Code:
    template<class T>
    CMatrix<T>::CMatrix(int cols, int rows){
    	m_Data = 0;
    	//init a new data matrix
    	InitDataMatrix(cols, rows);
    }
    
    template<class T>
    CMatrix<T>::~CMatrix(){
    	if (m_Data != 0){
    		ReleaseDataMatrix();
    	}
    }
    
    /**
     * Initialize data matrix[cols x rows] of type T
     */
    template<class T>
    void CMatrix<T>::InitDataMatrix(int cols, int rows){
    	//release old data matrix if any
    	if (m_Data != 0){
    		ReleaseDataMatrix();
    	}
    	//	
    	//get out if one dimension is zero;
    	if ((cols==0)||(rows==0)) return;
    	//
    	//set up members
    	m_Columns = cols;
    	m_Rows = rows;
    	//
    	//create new data matrix
    	m_Data = new T*[cols];
    	
    	for (int i = 0; i < cols; i++){
    		m_Data[i] = new T[rows];		
    	}
    }
    
    /**
     * Release data matrix 
     */
    template<class T>
    void CMatrix<T>::ReleaseDataMatrix(){	
    	//delete data
    	for(int i = 0; i < m_Columns; i++){
    		delete[] m_Data[i];
    	}
    	delete[] m_Data;
    	//
    	//reset members
    	m_Data = 0;
    	m_Columns = 0;
    	m_Rows = 0;
    }
    Now I have a CPixel class:

    Code:
    class CPixel{
    public:
    	CPixel(){
    	    m_BlueValue = 0;
    	    m_GreenValue = 0;
    	    m_RedValue = 0;
    	};
    	~CPixel(){};
    private:
    	byte m_BlueValue;
    	byte m_GreenValue;
    	byte m_RedValue;
    };
    The problem is that after creating a big matrix of CPixel (CMatrix<CPixel>) with the size, e.g., (600, 400), I got a runtime error at

    • delete[] m_Data[i] in CMatrix<T>::ReleaseDataMatrix()


    when the matrix is released. The error message is:

    Windows has triggered a breakpoint in MyApp.exe.

    This may be due to a corruption of the heap, and indicates a bug in MyApp.exe or any of the DLLs it has loaded.
    In addition, I don't get this error when creating a matrix of double (CMatrix<double>) even with the size 10000x10000.

    If you have any idea about that, please let me know!

    Thank you very much.

    .greenmile.

    p.s: I use Visual C++.Net 2005 on XP Professional

  2. #2
    Join Date
    Oct 2007
    Posts
    369
    The semi-colon after the definition of CPixel() and ~CPixel() is extraneous.

    Are you trying to define the template class signature in a .h and the body in a .cpp? Put everything in the .h.

    Give that a try. I took your code and made those adjustments and didn't have a problem deleting "CMatrix<CPixel> test(600, 400)" (my destructor ran as main went out of scope.

  3. #3
    Join Date
    Apr 2008
    Posts
    16
    Hi hendrixi,

    Thank you very much for your reply.

    I have found out my fault. Actually the error occurred because I assigned invalid values for pixels in the tested matrix, rather than the code I provided above. Sorry for my disturbing.

    Thanks and regards,

    .greenmile.

Similar Threads

  1. Matrix Problem in C++
    By emmanuella in forum C++
    Replies: 2
    Last Post: 11-23-2006, 06:07 PM
  2. Replies: 0
    Last Post: 11-11-2006, 05:42 AM
  3. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  4. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  5. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center