473,382 Members | 1,255 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,382 software developers and data experts.

Memory leakage in set class

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?
Jul 19 '05 #1
2 3713
"frustrated" <mm***@cornell.edu> wrote in message
news:96*************************@posting.google.co m...
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


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?

Jul 19 '05 #2
"frustrated" <mm***@cornell.edu> wrote in message
news:96*************************@posting.google.co m...

[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.

Jul 19 '05 #3

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

Similar topics

0
by: boy | last post by:
Hi all, I have created a simple template class as follow, but i encountered memory leakage on the base.Render(writer). Have all you of encountered the same problem? using System; using...
7
by: andylcx | last post by:
hi all: I have a code like below, is there any serious memory leakage in my code. I am confusion now but no idea how to fix it. In the member function of class A, I create a new object of class B...
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
18
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
2
by: boy | last post by:
Hi all, I have created a simple template class as follow, but i encountered memory leakage on the base.Render(writer). Have all you of encountered the same problem? using System; using...
0
by: kiran kumar | last post by:
Hi All, I am working on embedded python on C these days. I feel there is a memory leakage in this code. I have used our own memory pool and all the python code will use the heap from this memory...
14
by: madhawi | last post by:
i want to know that on what situation memory leakage happan and what is the solution to solve the problem of memory leakage.
3
by: Godzilla | last post by:
Hello, I have a program that create and pop an object off a queue, but it is experiencing some memory leakage. I have been unable to detect where the memory leakage occur. The strange thing is...
11
by: prpradip | last post by:
I have an ImageList (_imageList). In _imageList I have put large numbers of Icons. Now what I need is to get Handle of all Icons that I put in _imageList, so that I can destroy (DestoryIcon) them...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.