Hello,
I am using SGI's hash_map in such a way that there is an inherent
memory leak. Let me explain:
1- My hash_map takes char* as keys and one of my class, say myClass,
as data. I give the hash_map a comparator for the strings that
compares the contents of the string instead of the address of the
strings (by default hash_map compares the addresses of the strings
(!)).
2- When I want to insert an object of type myClass, I compute its key
using some of the characteristics (fields) of myClass converted into a
zero terminated sequence of chars. Then I insert the (key, object)
into the hash.
3- At a later point, suppose that I want to delete some of the objects
in the hash that satisfy some characteristics . As above, I compute a
key using those characteristics and then I remove the object
corresponding to those characteristics (if any) from the hash using
the hash's erase method. Then I delete the object, and I delete the
string used to lookup the object (the string computed in 3).
The problem is the following: I cannot delete the string computed in 2
to insert the object. If I delete it after insertion (at the end of
2), then the object cannot be looked up any more since the key cannot
be used in a comparison. I cannot delete it in 3 either because there
is no method in hash_map to "retrieve a key", i.e. I cannot retreive
the address of the string used for insertion.
My hash_map is used in a context of "massive" insert/delete so this
innocent-looking memory leak is critical in my case.
I was wondering how other users of the SGI hash_map got around this
issue.
Thank you,
Alex 2 1928
"rolivawdan eel" <ro***********@ hotmail.com> wrote in message
news:c1******** *************** ***@posting.goo gle.com... Hello,
I am using SGI's hash_map in such a way that there is an inherent memory leak. Let me explain:
1- My hash_map takes char* as keys and one of my class, say myClass, as data. I give the hash_map a comparator for the strings that compares the contents of the string instead of the address of the strings (by default hash_map compares the addresses of the strings (!)).
2- When I want to insert an object of type myClass, I compute its key using some of the characteristics (fields) of myClass converted into a zero terminated sequence of chars. Then I insert the (key, object) into the hash.
3- At a later point, suppose that I want to delete some of the objects in the hash that satisfy some characteristics . As above, I compute a key using those characteristics and then I remove the object corresponding to those characteristics (if any) from the hash using the hash's erase method. Then I delete the object, and I delete the string used to lookup the object (the string computed in 3).
The problem is the following: I cannot delete the string computed in 2 to insert the object. If I delete it after insertion (at the end of 2), then the object cannot be looked up any more since the key cannot be used in a comparison. I cannot delete it in 3 either because there is no method in hash_map to "retrieve a key", i.e. I cannot retreive the address of the string used for insertion.
My hash_map is used in a context of "massive" insert/delete so this innocent-looking memory leak is critical in my case.
I was wondering how other users of the SGI hash_map got around this issue.
Simple, use std::string instead of char* as your key.
BTW your assumption about 'no method to retrieve a key' is incorrect.
hash_map<char*, myClass> map;
char* key = map.find("some_ key")->first;
john
rolivawdaneel wrote: Hello,
I am using SGI's hash_map in such a way that there is an inherent memory leak. Let me explain:
1- My hash_map takes char* as keys and one of my class, say myClass, as data. I give the hash_map a comparator for the strings that compares the contents of the string instead of the address of the strings (by default hash_map compares the addresses of the strings (!)).
2- When I want to insert an object of type myClass, I compute its key using some of the characteristics (fields) of myClass converted into a zero terminated sequence of chars. Then I insert the (key, object) into the hash.
3- At a later point, suppose that I want to delete some of the objects in the hash that satisfy some characteristics . As above, I compute a key using those characteristics and then I remove the object corresponding to those characteristics (if any) from the hash using the hash's erase method. Then I delete the object, and I delete the string used to lookup the object (the string computed in 3).
The problem is the following: I cannot delete the string computed in 2 to insert the object. If I delete it after insertion (at the end of 2), then the object cannot be looked up any more since the key cannot be used in a comparison. I cannot delete it in 3 either because there is no method in hash_map to "retrieve a key", i.e. I cannot retreive the address of the string used for insertion.
My hash_map is used in a context of "massive" insert/delete so this innocent-looking memory leak is critical in my case.
I was wondering how other users of the SGI hash_map got around this issue.
Thank you, Alex
There are a few ways around this:
(a) [recommended] Use std::string instead of char* as the key. Strings
take care of their memory management automagically.
(b) If you are stuck with char* because some code is beyond your controll,
you could try something along the following lines:
...
Map::iterator iter = the_map.find( some_string );
const char * dummy = iter->first;
the_map.erase( iter );
delete [] dummy;
...
As you can see, iter->first retreives the key for you.
Best
Kai-Uwe This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Guy |
last post by:
Hi
It might take me a little time to explain this but here goes.
Firstly I'm not using the latest upto date python releases so my first
plan is to try more upto date rels of python and win32 libs, as this
has fixed problems for me in the past (Don't like doing this as it
usally breaks stuff and sys admin are slow to update software,
although they do a cracking job, just covering my back.)
I've created a build script for my companies...
|
by: jeevankodali |
last post by:
Hi
I have an .Net application which processes thousands of Xml nodes each
day and for each node I am using around 30-40 Regex matches to see if
they satisfy some conditions are not. These Regex matches are called
within a loop (like if or for). E.g.
for(int i = 0; i < 10; i++)
{
Regex r = new Regex();
|
by: Anton |
last post by:
{Willy Skjveland} Hi, how can I trace a Memory leak in aspnet_wp.exe?
{Rheena} One moment please while I search it for you. It may take me a few
moments
{Willy Skjveland} I need to find out which application and which dll and
asmx page that cause the problem.
{Rheena} May I know what operating system you are using?
|
by: Boni |
last post by:
I use 3-d party component. In this component I must pass a reference to my
object. The problem is that this component has an ugly bug.When this
component is disposed, it incorrectly don't delete the reference to my
object from one of its shared lists.And since the operation repeats many
times the leak is huge.
Is there a way to kill my object anyway?
Thanks a lot,
Boni
|
by: Johan Tibell |
last post by:
I would be grateful if someone had a minute or two to review my hash
table implementation. It's not yet commented but hopefully it's short
and idiomatic enough to be readable. Some of the code (i.e. the
get_hash function) is borrowed from various snippets I found on the
net. Thee free function could probably need some love. I have been
thinking about having a second linked list of all entries so that the
cost of freeing is in proportion to...
| |
by: Jim Land |
last post by:
Jack Slocum claims here
http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript-
memory-leaks/
that "almost every site you visit that uses JavaScript is leaking memory".
Anybody know anything about this?
Does *Javascript* leak memeory, or
does the *browser* leak memory?
|
by: jeungster |
last post by:
Hello,
I'm trying to track down a memory issue with a C++ application that
I'm working on:
In a nutshell, the resident memory usage of my program continues to
grow as the program runs. It starts off at a nice 4% of memory, then
slowly grows up to 50% and beyond. This translates to around 2 gigs
of physical memory, and that's really way more memory than this
program should be taking up.
|
by: Alexander Mahone |
last post by:
Hello, I'm looking for an hash function to be used for an hash table
that will contain structs of a certain kind. I've looked into
Sourceforge.net, but so far I've found only hash functions for strings
(string->index)...Do you know if there exist such a function
somewhere?
Thanks
|
by: Daniel Orner |
last post by:
Hi all,
I've been trying to pin down a memory leak in IE6 for several WEEKS
now. I've done my share of googling etc., and know all about common
leaks like circular references and closures, but have found nothing like
this.
This app is designed to run in a kiosk environment, so it has
things like a persistent frame which holds data as well as the visible
frame (actually an iframe). After 1,000 page transitions our app is up
by almost...
|
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...
|
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...
| |
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,...
|
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...
|
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();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |