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

Discussion in 'C++' started by sg71.cherub@gmail.com, Aug 21, 2006.

  1. Guest

    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.
     
    , Aug 21, 2006
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

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

    > whist the destructor can be correctly called.
     
    Rolf Magnus, Aug 21, 2006
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    wrote:

    > 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
     
    Kai-Uwe Bux, Aug 21, 2006
    #3
  4. Guest

    Many thx to the above!


    Kai-Uwe Bux wrote:
    > wrote:
    >
    > > 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
     
    , Aug 21, 2006
    #4
  5. wrote:
    > Many thx to the above!
    >
    >
    > Kai-Uwe Bux wrote:
    >> wrote:
    >>

    [...]

    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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 21, 2006
    #5
  6. Guest

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

    Tolga Ceylan
     
    , Aug 21, 2006
    #6
  7. Rolf Magnus Guest

    Victor Bazarov wrote:

    > 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".
     
    Rolf Magnus, Aug 21, 2006
    #7
  8. Rolf Magnus wrote:
    > Victor Bazarov wrote:
    >
    >> 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".


    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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 21, 2006
    #8
  9. Default User Guest

    Victor Bazarov wrote:

    > Rolf Magnus wrote:
    > > Victor Bazarov wrote:
    > >
    > > > 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".

    >
    > 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
     
    Default User, Aug 21, 2006
    #9
  10. "Default User" <> wrote...
    > [..] "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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 21, 2006
    #10
  11. Default User Guest

    Victor Bazarov wrote:

    > "Default User" <> wrote...
    > > [..] "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
     
    Default User, Aug 21, 2006
    #11
  12. Howard Guest

    "Default User" <> wrote in message
    news:...
    > Victor Bazarov wrote:
    >
    >> "Default User" <> wrote...
    >> > [..] "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
     
    Howard, Aug 21, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M.Ob

    ASP.net NOT Releasing Memory

    M.Ob, Jun 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,486
    Steve Drake
    Jun 10, 2004
  2. =?Utf-8?B?R2xlbm4gTWlsbGVy?=

    ASP.NET Not Releasing Memory

    =?Utf-8?B?R2xlbm4gTWlsbGVy?=, Sep 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    453
    =?Utf-8?B?R2xlbm4gTWlsbGVy?=
    Sep 23, 2004
  3. Arsalan
    Replies:
    7
    Views:
    781
    Rob Nicholson
    Apr 2, 2005
  4. Richard Herring

    Re: free() is not releasing memory, help

    Richard Herring, Apr 5, 2004, in forum: C++
    Replies:
    2
    Views:
    477
    Richard Herring
    Apr 6, 2004
  5. lebo
    Replies:
    4
    Views:
    524
    John J. Lee
    Sep 26, 2003
Loading...

Share This Page