473,513 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Map erase memory problem

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

Jun 15 '06 #1
1 2512
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.
Jun 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9948
by: s | last post by:
Here is a snippet of my code: <code> list< MyClass * >outgoing_pool; MyClass* GetWaitingObject(string freq) { MyClass *temp_ptr = NULL; list<MyClass*>::iterator i;
0
1215
by: Tero Toivanen | last post by:
Dear experts, I am doing code to Solaris 9 system with C++. I get every now and then segmentation fault in the following code that removes heading and trailing white spaces (mLineStr is of...
1
1837
by: Tero Toivanen | last post by:
Dear experts, I am doing code to Solaris 9 system with Forte 6 Update 2 C++ compiler.. I get every now and then segmentation fault in the following code that removes heading and trailing...
17
7502
by: Gernot Frisch | last post by:
restart: for (std::map<x,y>::iterator it = m.begin(); it!=m.end(); ++it) { if( it->second.isbad() ) { std::map<x,y>::iterator next = it; ++next; m.erase(it); it=next; // goto restart;
20
2700
by: Tom van Stiphout | last post by:
I'm about to write a function like below, which I'm going to call a lot of times. So I care about possible memory leaks. I think whether I should use Erase or not depends on whether Split creates...
3
6230
by: Jeff | last post by:
I have a question about potential memory leakage in a 2 dim array I have. If I say: vector< vector<int> > vec; vec.push_back( vector<int>() ); // initialize the row vec.push_back(1);...
16
3570
by: Frank Neuhaus | last post by:
Hi I have some std list, I'd like to traverse. During the traversal, I want to conditionally delete some objects. My code for that is like this right now: for (std::list<myStruct>::iterator...
3
1730
by: AbrahamLincolnIllinois | last post by:
Hi all. I have a list of pointers to a complicated object. When I erase() a member of that list, the little blob of memory that contains the pointer is deleted, I think. But the object pointed...
3
2346
by: ma740988 | last post by:
For discussion purposes, assume the vector ivec contains 67108864 (67 million elements) elements. Lets futher assume nstart and end equal 1008000 and 11088000 respectively. The question. ...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.