473,671 Members | 2,279 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(someVa lueofT1);

i->second.erase(T 2);

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 2520
In article <11************ **********@y41g 2000cwy.googleg roups.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(someVa lueofT1);

i->second.erase(T 2);

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(someVa lueofT1);
cout << "i found\n";
i->second.erase(s omeValueofT2);
cout << "someValueo fT2 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
9959
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
1222
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 type std:string): ------
1
1845
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 white spaces (global variable mLineStr is of type std::string, and the program has just the main thread):
17
7535
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
2719
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 a dynamic array (similar to ReDim a(2)). I have a gut feeling it does. Opinions? Dim a() as String a = Split("part1|part2|part3", "|") ' code that uses a goes here Erase a
3
6240
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); vec.push_back(2);
16
3583
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 it=myList.begin();it!=myList.end();) { if (it->someCondition) {
3
1741
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 to is not deleted, true? It is not an auto_ptr or any kind of wrapped pointer. Further, I assume that the iterator I am holding is no longer valid once I delete the element. I can't safely erase the member first,
3
2359
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. What's the _fastest_ way to erase element numbers less than 1008000 and element numbers greater than 11088000. Current approach. typedef std::vector < int INT_VEC ;
0
8481
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8602
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8672
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6234
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5702
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1814
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.