Memory leakage in set class

F

frustrated

Before I begin, I must first make the following disclaimer: Although I have
considerable programming experience, I do not consider myself by any means
to be an expert C++ programmer. The following may be nothing more than a
relection of my ignorance. If what I describe is not an actual bug, I would
be very appreciative if you could briefly explain to me how I can de-allocate
memory allocated by a set class, since everything I have tried is in vain
and every computer scientist I have asked seems as dumbfounded as I.


I am running g++ 2.96 on a i386 redhat linux platform. I think I discovered
a bug. I compiled and ran the following program.

#include <set>

int main()
{
unsigned long j;
set<unsigned long> *o = new set<unsigned long>();
for(j = 1; j <= 1000000; j++)
{
(*o).insert(j);
}
(*o).clear();
delete o;
while(1);
}

Using top, I monitored memory usage and noticed the delete operation did
not free the 24 MB allocated by the multiple calls to insert in the for
loop. This problem seems confined to the set and map classes. No matter
what I seem to do, I cannot de-allocate memory allocated by the set or map
classes. I do not enounter the problem with the vector class. For example,
I did not observe using top any memory leakage when I compiled and ran

#include <vector>

int main()
{
unsigned long j;
vector<unsigned long> *o = new vector<unsigned long>();
for(j = 1; j <= 10000000; j++)
{
(*o).push_back(j);
}
(*o).clear();
delete o;
while(1);
}

I know that clear alone at least for a vector does not de-allocate memory
since it merely erases the elements without altering the capacity.
Nevertheless, shouldn't the delete operation, whether it is applied to
an empty vector, set, or map, always perform the necessary de-allocation?
 
M

Marcin Vorbrodt

frustrated said:
Before I begin, I must first make the following disclaimer: Although I have
considerable programming experience, I do not consider myself by any means
to be an expert C++ programmer. The following may be nothing more than a
relection of my ignorance. If what I describe is not an actual bug, I would
be very appreciative if you could briefly explain to me how I can de-allocate
memory allocated by a set class, since everything I have tried is in vain
and every computer scientist I have asked seems as dumbfounded as I.


I am running g++ 2.96 on a i386 redhat linux platform. I think I discovered
a bug. I compiled and ran the following program.

#include <set>

int main()
{
unsigned long j;
set<unsigned long> *o = new set<unsigned long>();
for(j = 1; j <= 1000000; j++)
{
(*o).insert(j);
}
(*o).clear();
delete o;
while(1);
}

Try this:

int main()
{
for(;;) {
unsigned long j;
set<unsigned long> *o = new set<unsigned long>();
for(j = 1; j <= 1000000; j++)
{
(*o).insert(j);
}
(*o).clear();
delete o;
}
while(1);
}

Or this:

int main()
{
set<unsigned long> *o = new set<unsigned long>();
for(;;) {
unsigned long j;
for(j = 1; j <= 1000000; j++)
{
(*o).insert(j);
}
(*o).clear();
}
delete o;
while(1);
}

Both should run just fine (in the infinite loop) and not cause any memory
leaks. I think "top" does not report memory has been dealocated because of
OS caching nonsense going on behind the scenes. If you run those two
samples, i think eventually system will start flushing its cache... this is
a wild guess though:)


Martin
 
K

Klaus Eichner

[Snip]
If what I describe is not an actual bug, I would be very
appreciative if you could briefly explain to me how
I can de-allocate memory allocated by a set class
[Snip]

I am running g++ 2.96 on a i386 redhat linux platform

#include <set>

int main()
{
unsigned long j;
set<unsigned long> *o = new set<unsigned long>();
for(j = 1; j <= 1000000; j++)
{
(*o).insert(j);
}
(*o).clear();
delete o;
while(1);
}

Using top, I monitored memory usage and noticed the delete operation did
not free the 24 MB allocated by the multiple calls to insert in the for
loop.

This is a problem, but this is not a bug as far as the standard is
concerned.
Jim Hyslop gave a reply to that problem in comp.lang.c++
(Subject: 'What does delete do with memory?' Date 12 Mai 2000,
see http://tinyurl.com/qjjy)

You might find a solution to your problem in your g++ 2.96/Linux
documentation.
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top