In article <11**********************@y41g2000cwy.googlegroups .com>,
"Milind" <ma******@gmail.com> wrote:
Hello,
I have a std::map which looks like
typedef map<T1, map<T2, T3> > myMap;
as a part of my code i delete a pair from the myMap if the nested map
is empty. for example a sequence in my code looks like:
myMap xyz;
myMap::iterator i = xyz.find(someValueofT1);
i->second.erase(T2);
if(i->second.empty()) {
xyz.erase(i);
}
T1, T2 and T3 are all reference counting pointers (smart pointers)
which will delete themselves after i have no references for them.
Now, the when i call erase, delete is not called for any of the
members, how do i ensure that the memeory allocated for the nested map
is cleaned? Is is cleaned in the first place??
Any pointers, links??
Am I missing someting too obvious??
Did you write your own smart pointer?
class T1 { public: ~T1() { cout << "~T1\n"; } };
class T2 { public: ~T2() { cout << "~T2\n"; } };
class T3 { public: ~T3() { cout << "~T3\n"; } };
bool operator<( T1 lhs, T1 rhs ) {
return &lhs < &rhs;
}
bool operator<( T2 lhs, T2 rhs ) {
return &lhs < &rhs;
}
typedef map<T1, map<T2, T3> > myMap;
test_(foo) {
myMap xyz;
T1 someValueofT1;
T2 someValueofT2;
xyz[someValueofT1][someValueofT2] = T3();
cout << "map loaded\n";
myMap::iterator i = xyz.find(someValueofT1);
cout << "i found\n";
i->second.erase(someValueofT2);
cout << "someValueofT2 erased\n";
if(i->second.empty()) {
xyz.erase(i);
cout << "i erased\n";
}
}
All the destructors are called as expected in the above code.