473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SGI hash: existence of a memory leak impossible to solve?

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
Jul 22 '05 #1
2 1897

"rolivawdaneel" <ro***********@hotmail.com> wrote in message
news:c1**************************@posting.google.c om...
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
Jul 22 '05 #2
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
Jul 22 '05 #3

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

Similar topics

3
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...
20
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...
9
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...
13
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...
21
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...
3
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". ...
9
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...
24
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...
18
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
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...

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.