Possible mem leak?
Posted
by
LCD Fire
on Stack Overflow
See other posts from Stack Overflow
or by LCD Fire
Published on 2012-09-14T14:23:21Z
Indexed on
2012/09/14
15:38 UTC
Read the original article
Hit count: 424
I'm new to the concept so don't be hard on me. why doesn't this code produce a destructor call ? The names of the classes are self-explanatory. The SString will print a message in ~SString(). It only prints one destructor message.
int main(int argc, TCHAR* argv[])
{
smart_ptr<SString> smt(new SString("not lost"));
new smart_ptr<SString>(new SString("but lost"));
return 0;
}
Is this a memory leak? The impl. for smart_ptr is from here
edited:
//copy ctor
smart_ptr(const smart_ptr<T>& ptrCopy)
{
m_AutoPtr = new T(ptrCopy.get());
}
//overloading = operator
smart_ptr<T>& operator=(smart_ptr<T>& ptrCopy)
{
if(m_AutoPtr)
delete m_AutoPtr;
m_AutoPtr = new T(*ptrCopy.get());
return *this;
}
© Stack Overflow or respective owner