Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Beginner
Oct 12, 2000



Application Security Testing: An Integral Part of DevOps

New and Delete

In my class declaration I have a member:

CSomeUsefulClass* m_one;

In my implementation file, there is a function:


CSomeUsefulClass* m_two;
m_two = new CSomeUsefulClass(param1, param2);
m_one = m_two;
//do my thing...


then in the destructor of my class:

  delete m_one; 

Will this program cause a memory leak?

No, your code won't cause a memory leak because your destructor deletes the dynamically allocated object. However, I'm wary about the design. First of all, the object is allocated only when DoMyThing() is called. Are you sure it's alyways called? And if so, what happens if you call it more than once? If the pointer already points to an object, you're causing a memory leak. Finally, using two pointers that point to the same object is very dangerous.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date