S
sg71.cherub
Hi All,
I have encapsulate CvMat of OpenCV into my own matrix class as the
following:
class CVMatrix
{
//== Fields
private:
unsigned m_Width;
unsigned m_Height;
CvMat* m_Matrix;
//== Constructors and destructor
public:
CVMatrix(); // empty constructor
CVMatrix(int width, int height); // formal constructor
CVMatrix(const CVMatrix& matrix); // copy constructor
~CVMatrix();
}; // CVMatrix
Constructor is defined as (e.g. CVMatrix(int width, int height)
CVMatrix::CVMatrix(int width, int height)
{
if (width <= 0 || height <= 0)
exit(0);
m_Width = width;
m_Height = height;
m_Matrix = cvCreateMat(m_Height, m_Width, FLOAT);
}
Destructor is defined as:
CVMatrix::~CVMatrix()
{
cvReleaseMat(&m_Matrix);
}
If I use the matrix as initializing an instance:
CVMatrix a(4, 4);
matrix a can be released at the end of program.
However, if the matrix is initialized as a pointer:
CVMatrix* a = new CVMatrix(4, 4);
The destructor would never been called, and causes memory leak problem.
Moreover, when the matrix array is required, such as
CVMatrix** matrixArray = new CVMatrix*[length];
for (int i = 0; i < length; i ++)
matrixArray = new CVMatrix(4, 4);
There would be severe memory leak problem since the destructor would
never been called for all those CVMatrix(s).
Would be appreciated should any body teach me the formal way using the
object pointer, as well as the object array, whist the destructor can
be correctly called.
I have encapsulate CvMat of OpenCV into my own matrix class as the
following:
class CVMatrix
{
//== Fields
private:
unsigned m_Width;
unsigned m_Height;
CvMat* m_Matrix;
//== Constructors and destructor
public:
CVMatrix(); // empty constructor
CVMatrix(int width, int height); // formal constructor
CVMatrix(const CVMatrix& matrix); // copy constructor
~CVMatrix();
}; // CVMatrix
Constructor is defined as (e.g. CVMatrix(int width, int height)
CVMatrix::CVMatrix(int width, int height)
{
if (width <= 0 || height <= 0)
exit(0);
m_Width = width;
m_Height = height;
m_Matrix = cvCreateMat(m_Height, m_Width, FLOAT);
}
Destructor is defined as:
CVMatrix::~CVMatrix()
{
cvReleaseMat(&m_Matrix);
}
If I use the matrix as initializing an instance:
CVMatrix a(4, 4);
matrix a can be released at the end of program.
However, if the matrix is initialized as a pointer:
CVMatrix* a = new CVMatrix(4, 4);
The destructor would never been called, and causes memory leak problem.
Moreover, when the matrix array is required, such as
CVMatrix** matrixArray = new CVMatrix*[length];
for (int i = 0; i < length; i ++)
matrixArray = new CVMatrix(4, 4);
There would be severe memory leak problem since the destructor would
never been called for all those CVMatrix(s).
Would be appreciated should any body teach me the formal way using the
object pointer, as well as the object array, whist the destructor can
be correctly called.