to deallocate the memory

Discussion in 'C++' started by sugankanya86@gmail.com, Apr 3, 2008.

  1. Guest

    Hi all,
    there is a code snippet like this

    this -> PClient = new IPCAMAClient (&tcpProfile, clientPriority);

    if (!this -> PClient -> isValid())
    {
    // The object is invalid, the connection was not
    established
    delete this -> PClient;
    this -> PClient = NULL;
    }
    else
    {
    // The Object is valid
    this -> Valid = TRUE;
    }

    in the above case, whether the memory alloted by the new operator is
    released perfectly or not?
    since there is no delete operator in the else part,is this a memory
    leak?
     
    , Apr 3, 2008
    #1
    1. Advertising

  2. Sharad Guest

    <> wrote in message

    > Hi all,
    > there is a code snippet like this
    >
    > this -> PClient = new IPCAMAClient (&tcpProfile, clientPriority);
    >
    > if (!this -> PClient -> isValid())
    > {
    > // The object is invalid, the connection was not
    > established
    > delete this -> PClient;
    > this -> PClient = NULL;
    > }
    > else
    > {
    > // The Object is valid
    > this -> Valid = TRUE;
    > }
    >
    > in the above case, whether the memory alloted by the new operator is
    > released perfectly or not?
    > since there is no delete operator in the else part,is this a memory
    > leak?


    Is this the complete code? Perhaps not. Post the complete minimal code.
     
    Sharad, Apr 3, 2008
    #2
    1. Advertising

  3. Ron Natalie Guest

    wrote:
    > Hi all,
    > there is a code snippet like this
    >
    > this -> PClient = new IPCAMAClient (&tcpProfile, clientPriority);
    >
    > if (!this -> PClient -> isValid())
    > {
    > // The object is invalid, the connection was not
    > established
    > delete this -> PClient;
    > this -> PClient = NULL;
    > }
    > else
    > {
    > // The Object is valid
    > this -> Valid = TRUE;
    > }
    >
    > in the above case, whether the memory alloted by the new operator is
    > released perfectly or not?
    > since there is no delete operator in the else part,is this a memory
    > leak?


    Generally you don't have to put this-> on access of members.

    As little as you posted, it looks OK. The fact that you are
    for some reason dynamically allocating memory indicates that
    you should serious consider making sure that there are no
    exceptions that can intervene between the new and the delete
    and that the outer class has proper destruction and copying
    semantics.
     
    Ron Natalie, Apr 3, 2008
    #3
    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. Eric Lilja
    Replies:
    2
    Views:
    3,614
    Victor Bazarov
    Mar 14, 2005
  2. Replies:
    20
    Views:
    1,366
    Heiko Wundram
    May 12, 2006
  3. Tony Johansson
    Replies:
    3
    Views:
    330
    Old Wolf
    Aug 13, 2005
  4. pereges
    Replies:
    6
    Views:
    305
    santosh
    Jul 7, 2008
  5. alessio211734

    handle deallocate memory on return

    alessio211734, Jun 30, 2009, in forum: C++
    Replies:
    3
    Views:
    570
    Thomas J. Gritzan
    Jul 3, 2009
Loading...

Share This Page