question on initializing a pointer to an object and releasing the memory.

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.
 
R

Rolf Magnus

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();

You probably also need an assignment operator.
}; // 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.

The destructor is called as soon as you delete your matrix. And you should
always delete the objects you get from new.
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,


delete a;
as well as the object array,

for (int i = 0; i < length; ++i)
delete matrixArray;
delete [] matrixArray;
 
K

Kai-Uwe Bux

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.

The destructor will be called when you say

delete a;

which you should do for any pointer that you new.
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).


Here you need to says

delete matrixArray;

for each index i, and then, you should do:

delete[] matrixArray;

because this is how get rid of arrays that you new[]ed.

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.

The best way of dealing with pointers is not to. You may want to consider
using tr1::shared_ptr<CVmatrix> instead which will call the destructor of
the pointee and release the allocated memory as soon as the last pointer to
the pointee goes out of scope. (At least that is what it does as long as
you do not create cyclic data structures.)

Pointers are about the most tricky aspect of C++: every new has to be
matched with a delete along each path of execution. Since exceptions can
divert the path of execution on almost any given line, this gets out of
control rather quickly. Therefore, it is best practice to use new only in
constructors and place the corresponding delete within the destructor. This
way proper matching is guaranteed by the language. If you feel the need for
a pointer in your program, very likely you want to use a smart pointer
instead.


Best

Kai-Uwe Bux
 
S

sg71.cherub

Many thx to the above!


Kai-Uwe Bux said:
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.

The destructor will be called when you say

delete a;

which you should do for any pointer that you new.
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).


Here you need to says

delete matrixArray;

for each index i, and then, you should do:

delete[] matrixArray;

because this is how get rid of arrays that you new[]ed.

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.

The best way of dealing with pointers is not to. You may want to consider
using tr1::shared_ptr<CVmatrix> instead which will call the destructor of
the pointee and release the allocated memory as soon as the last pointer to
the pointee goes out of scope. (At least that is what it does as long as
you do not create cyclic data structures.)

Pointers are about the most tricky aspect of C++: every new has to be
matched with a delete along each path of execution. Since exceptions can
divert the path of execution on almost any given line, this gets out of
control rather quickly. Therefore, it is best practice to use new only in
constructors and place the corresponding delete within the destructor. This
way proper matching is guaranteed by the language. If you feel the need for
a pointer in your program, very likely you want to use a smart pointer
instead.


Best

Kai-Uwe Bux
 
V

Victor Bazarov

Many thx to the above!
[...]

Please don't top-post.

Please don't post just to say "thanks"

Please don't quote the entire message when you don't refer to any of it.

What or who is "the above" you're referring to? Your statement is the
very first line in your message.

V
 
T

tolgaceylanus

Also, if you write your own copy constructor for a class, it's good
practice to also provide an
assignment operator.

Tolga Ceylan
 
R

Rolf Magnus

Victor said:
Please don't post just to say "thanks"

Well, if the OP doesn't answer at all, it always feels like he ignored the
replies to his posting. I think it's a good idea to say "thanks".
 
V

Victor Bazarov

Rolf said:
Well, if the OP doesn't answer at all, it always feels like he
ignored the replies to his posting. I think it's a good idea to say
"thanks".

I think it's a waste of bandwidth and my time. I see another post by
the OP, and open it, thinking it might be another question on the same
subject, or an explanation, or the final review of the proposed fix...
And what do I find? A top-posted thank-you note with the rest of it
hanging below! Annoying. Thank in advance and be done with it. If
one needs a confirmation of whether the OP read the replies, send them
a private e-mail on that. I couldn't care less if they read it or not,
in all honesty.

V
 
D

Default User

Victor said:
I think it's a waste of bandwidth and my time. I see another post by
the OP, and open it, thinking it might be another question on the same
subject, or an explanation, or the final review of the proposed fix...
And what do I find? A top-posted thank-you note with the rest of it
hanging below! Annoying. Thank in advance and be done with it. If
one needs a confirmation of whether the OP read the replies, send them
a private e-mail on that. I couldn't care less if they read it or
not, in all honesty.

I disagree with your main point. "Thank you" messages don't bother me
in the slightest, and I often acknowledge responses that I get to
questions.

Your point about top-posting is well taken, of course, but that would
apply to any response.




Brian
 
V

Victor Bazarov

Default User said:
[..] "Thank you" messages don't bother me
in the slightest, and I often acknowledge responses that I get to
questions.

Thank you notes add nothing technical to the discussion. They are
unnecessary if the original post has a "thank you in advance". Any
acknowledgement can come in a brief technical summary of the previous
points and definitely doesn't need to include the entire previous post.

V
 
D

Default User

Victor said:
Default User said:
[..] "Thank you" messages don't bother me
in the slightest, and I often acknowledge responses that I get to
questions.

Thank you notes add nothing technical to the discussion.

Lots of what gets posted adds no technical details. Like, for instance,
complaining about thanks. Or complaining about complaining about thanks.
They are
unnecessary if the original post has a "thank you in advance". Any
acknowledgement can come in a brief technical summary of the previous
points and definitely doesn't need to include the entire previous
post.

That's your opinion. It's not mine. Acknowledgement from the recipient
lets the contributors know that the specific advice given was useful.
That can't be done with a TIA.




Brian
 
H

Howard

Default User said:
Victor said:
Default User said:
[..] "Thank you" messages don't bother me
in the slightest, and I often acknowledge responses that I get to
questions.

Thank you notes add nothing technical to the discussion.

Lots of what gets posted adds no technical details. Like, for instance,
complaining about thanks. Or complaining about complaining about thanks.
They are
unnecessary if the original post has a "thank you in advance". Any
acknowledgement can come in a brief technical summary of the previous
points and definitely doesn't need to include the entire previous
post.

That's your opinion. It's not mine. Acknowledgement from the recipient
lets the contributors know that the specific advice given was useful.
That can't be done with a TIA.

Just to continue this OT fun...

The TIA concept reminds me of something I learned in my youth, which you
might have heard before:

"For all [the help] that I am about to receive, may I be truly thankful."
:)

-H
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top