why does returning a map.insert().second introduce unreachable memory?
The following code:
~A()
{
for (itr = mymap.begin(); itr != mymap.end() ++itr)
{
delete itr->second //the map look like this <std::string , T*>
}
}
A::Addnew(std::string name)
{
return mymap.insert(std::pair<name, new T>).second;
}
introduces a memory leak, but if I change the AddNew() member function to:
itr = mymap.find(name);
if(itr == mymap.end())
{
return mymap.insert(std::pair<name, new T>).second;
}
then there is no memory leak.
It seems like if I called the first case accidentally, I will introduce
lots of new T, but my mymap.size() cannot keep track of it. Can anyone
explain this?
No comments:
Post a Comment