473,406 Members | 2,769 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.

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 3715
"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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.