Pete
Pete

Reputation: 10871

What happens to dynamically allocate objects inside of an object that is deleted?

When I use the delete keyword on an object is that objects destructor executed? What if the objected I deleted contains other pointers and I don't delete them, is that a memory leak?

I get a confused a bit sometimes over when to use delete. The problem is worst for me when I pass things around. I don't know when it's safe to use delete for fear of eliminating an object that is being pointed to from somewhere else.

Upvotes: 1

Views: 520

Answers (6)

Alecs
Alecs

Reputation: 2316

When you call delete someObect; happens something like this:

if(someObect != NULL)
{
someObect->~ClassName();// you did ClassName someObect = new ClassName(); 
operator delete(someObect);
}

And operator delete do as I understand the same thing that free do - just frees(deallocates) the memory. But remember, that if you used new you cannot use free, you must use delete and only it.

Upvotes: 0

dubnde
dubnde

Reputation: 4441

Yes it is a leak. See if the Freestore management FAQ can add some insight to you understanding.

Upvotes: 0

Doug T.
Doug T.

Reputation: 65649

When I use the delete keyword on an object is that objects destructor executed?

Yes

What if the objected I deleted contains other pointers and I don't delete them, is that a memory leak?

Yes, unless someone else also has a pointer and that someone else is in charge of deleting them. You can also have problems due to double deletion. If you delete a pointer given to you, but someone else has a pointer to that memory, then his pointer now points at nothing. When he goes to use that pointer, his program could crash.

I don't know when it's safe to use delete for fear of eliminating an object that is being pointed to from somewhere else.

You're not the only one. Its important to establish protocols and conventions between parts of your code about who owns what. What is "producing" objects. What is "consuming" them. You may also wish to use tools such as boost::shared_ptr and boost::weak_ptr to allow for reference counting.

Upvotes: 1

JSBձոգչ
JSBձոգչ

Reputation: 41388

When I use the delete keyword on an object is that objects destructor executed?

Yes. That's what the destructor is for.

What if the objected I deleted contains other pointers and I don't delete them, is that a memory leak?

Yes. This is one of the most common sources of memory leaks.

I don't know when it's safe to use delete for fear of eliminating an object that is being pointed to from somewhere else.

This is a hard problem. There is no system that solves it perfectly, but you can get pretty far by using reference-counted smart pointers, and by reducing the number of objects that are shared.

Upvotes: 3

finsprings
finsprings

Reputation: 130

If you use new, you must use delete. That will trigger the instance's destructor to be called. If that instance new'd any objects in its constructor (or later) it should delete them in its destructor.

Also, if you new up arrays (new char[20] etc) you must use 'delete []' when deleting, else the behaviour is undefined.

You can avoid a lot of the pain by using std::tr1::shared_ptr, or boost::shared_ptr, which will do reference counting and do the deletes for you, that is instead of:

Foo *pFoo = new Foo;

do

std::tr1::shared_ptr<Foo> pFoo(new Foo);

then you don't need to do a delete: when the shared_ptr's communal reference count goes to zero it will do the delete for you.

Upvotes: 0

Andrew Rasmussen
Andrew Rasmussen

Reputation: 15109

That is a memory leak. That's a very common problem. When you delete an object that you used new to allocate memory for, it will call that objects destructor. The destructor is where you are supposed to provide the implementation to clean up (delete) all of the memory you may have allocated during the lifetime of that object.

Upvotes: 0

Related Questions