How to free memory from a list of classes
Posted
by Jason Rowe
on Stack Overflow
See other posts from Stack Overflow
or by Jason Rowe
Published on 2010-04-05T16:55:22Z
Indexed on
2010/04/05
17:03 UTC
Read the original article
Hit count: 320
Say I have two classes created work and workItem.
CWorker *work = new CWorker();
CWorkItem *workItem = new CWorkItem();
The work class has a public list m_WorkList and I add the work item to it.
work->m_WorkList.push_back(workItem);
If I just delete work
if(work != NULL)
delete work;
Do I need to loop through the list in the destructor like the following? Any better way to do this? Could I use clear instead?
while(m_WorkList.size())
{
CWorkItem *workItem = m_WorkList.front();
m_WorkList.pop_front();
if(workItem)
delete workItem;
}
© Stack Overflow or respective owner