How could I free up a linked list that contains dynamically allocated objects?
I try to use list<class*>
lists, but then I could not use the insert()
function to insert object to the list. Does anyone know what is the cause?
views:
540answers:
8The solution used by the STL is to have the container own the objects it contains. In this scenario each node would be responsible for deallocating it's object when it's destructor is called. You're still responsible for deallocating the copy of the object you pass in.
Edit
If you're attempting to use the stl List and encountering an error, it might be the case that the object you're trying to place inside does not implement a copy constructor. This can happen if you try to stick things other than pointers into the list. If you're comfortable with pointers you might consider storing them in your containers instead of the objects themselves. This will require that you call 'delete' yourself on the pointer when you're done with it. This is typically done by calling 'delete' on the pointer contained in a node when you remove it or by walking the list when you're done with it and calling 'delete' on the contents of each node.
how could I free up linked list that contain dynamically allocated object?
Walk the list, deleting the contained object for each link, or better in some cases, write a link dtor that deletes the contained object.
I try to use list lists, but then I could not use insert()function to insert object to the list. Does anyone know what is the cause?
Not without seeing your code.
Start your project in language/environment, which having an automatic memory management (Garbage collection). Then things like that will be the least which you care about. It will save your time to put your efforts into something else.
You will have to iterate the list and delete
the dynamically allocated objects, before clearing the list.
When using containers of newed pointers, please remember to delete the pointers before the container is destroyed.
About problem using insert()
: I am not very sure what is the problem you are facing. From the explanation, I assume, you are trying to insert objects of Class
into list where as the list contains the pointer to Class
. Could you please elaborate the problem with insert?
Pseudo code for deleting the dynamically allocated objects:
version 1: Simple
list<Class*>::const_iterator iter = m_ClassList.begin();
list<Class*>::const_iterator endIter = m_ClassList.end();
for(;iter!= endIter ; ++iter)
{
delete *iter;
}
ClassList.clear();
version 2: A better version using for_each
struct DeleteClassObject
{
//Functor
template<typename T>
void operator()(const T* ptr) const
{
delete ptr;
}
}
//loops through list and deletes the dynamically allocated objects using
//functor DeleteClassObject
for_each( m_ClassList.begin(), m_ClassList.end(), DeleteClassObject ());
DeleteClassObject.clear()
std::list<boost::shared_ptr<YourType> >
will automatically call YourType::~YourType
on each (smart) pointer in the list, when the list is deleted. And if you erase one list element, it will call YourType::~YourType
for that one element. You can still call list.insert(new YourType
)
Link: www.boost.org
EDIT: THIS IS WRONG, SORRY
If you're using std::list then there's a clear function to delete all objects you put in. std::list test; test.clear(); Oh, and .clear() also invokes the destructors. To use the insert function of such lists you need to specify the position via an iterator. test.insert(test.end(), 5); But there's more than 1 insert function, here you can find more details
EDIT
Could somebody leave a comment when downvoting?
You could use ptr_list from Boost
http://www.boost.org/doc/libs/1_38_0/libs/ptr_container/doc/ptr_list.html