Re: STL Memory leak?
schliz wrote:
As I am saving pointer of CScreen in the list. Do I have to delete the
CScreens objects manually?
Example 1:
CScreen* newScreen = new CScreen();
list<CScreen*> m_screenList;
m_screenList.push_back(newScreen);
m_screenList.clear(); --> does it create a memory leak?
No, since you still have a pointer to the screen in newScreen, and could
do something like
delete newScreen;
next. This will create a memory leak for most sane definitions of CScreen:
void f()
{
CScreen* newScreen = new CScreen();
list<CScreen*> m_screenList;
m_screenList.push_back(newScreen);
m_screenList.clear();
}
Why would you expect list to treat CScreen* specially, or assume that it
can delete the pointed-to object? For all it knows, you've done
CScreen s;
list<CScreen*> l;
l.push_back( &s );
l.clear();
If it (tried to) delete the pointed-to object, then its usefulness would
be reduced.
By the way, your naming convention is very verbose, and thus obscures
things. The scope of a variable should play a part in the length of its
name; smaller scope, shorter name. But this is just a style issue.