Map erase memory problem

M

Milind

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??

~M
 
D

Daniel T.

"Milind said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top