to deallocate the memory

S

sugankanya86

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?
 
S

Sharad

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

Ron Natalie

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.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top