473,665 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory leak...

I'm pretty sure that the program bellow contains a memory leak. After
deleting the "hand_vecto r" instance, not all memory is released (only about
30%). It appears to me as though the instances of "hand" are still hanging
around. Are they not on the heap? Shouldn't they be released along with the
"hand_vecto r" instance? Thanks for any help.

d

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class hand {
public:
vector<int> my_ints;
};

class hand_vector {
public:
vector<hand> myhands;
};

int main(int argc, char *argv[])
{

system("PAUSE") ;

hand_vector *myvector = new hand_vector;
hand v_stub;

for (int i=1; i<=5000000; i++) {
myvector->myhands.push_b ack(v_stub);
};

system("PAUSE") ;

vector<hand>::i terator j;

for (j=myvector->myhands.begin( ); j!=myvector->myhands.end( ); j++) {
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
};

system("PAUSE") ;

delete myvector;

system("PAUSE") ;
return EXIT_SUCCESS;
}


Jul 23 '05 #1
4 1390

deancoo wrote:
I'm pretty sure that the program bellow contains a memory leak. After deleting the "hand_vecto r" instance, not all memory is released (only about 30%). It appears to me as though the instances of "hand" are still hanging around. Are they not on the heap? Shouldn't they be released along with the "hand_vecto r" instance? Thanks for any help.


How do you know? A memory leak occurs when memory cannot be reused
within
the same program. You cannot determine that from the outside. Many STL
implementations will not return memory tot he OS, but keep it available
for the next allocation. That's not a memory leak. It's not just the
STL.
Even std::malloc( ) can do that.

HTH,
Michiel Salters

Jul 23 '05 #2

"deancoo" <s2*******@yaho o.ca> skrev i en meddelelse
news:iTiQd.5900 0$L_3.21722@clg rps13...
I'm pretty sure that the program bellow contains a memory leak. After
deleting the "hand_vecto r" instance, not all memory is released (only
about
30%). It appears to me as though the instances of "hand" are still
hanging
around. Are they not on the heap? Shouldn't they be released along with
the
"hand_vecto r" instance? Thanks for any help.
Apart from your decision to allocate myvector dynamically (and i assume this
was just for testing purposes), I would give your program a ...ehhh... hands
up.

/Peter
d

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class hand {
public:
vector<int> my_ints;
};

class hand_vector {
public:
vector<hand> myhands;
};

int main(int argc, char *argv[])
{

system("PAUSE") ;

hand_vector *myvector = new hand_vector;
hand v_stub;

for (int i=1; i<=5000000; i++) {
myvector->myhands.push_b ack(v_stub);
};

system("PAUSE") ;

vector<hand>::i terator j;

for (j=myvector->myhands.begin( ); j!=myvector->myhands.end( ); j++) {
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
};

system("PAUSE") ;

delete myvector;

system("PAUSE") ;
return EXIT_SUCCESS;
}

Jul 23 '05 #3
Hello, I have cheked this code on my machine, there are no memory
leaks. Maybe you are using bad STL implementation or something else.

P.S.: I've checked this code on Windows 2000 SP2, compile with MS
Visual Studio 2003 (.NET). It's ok.

-- SnaiL

Jul 23 '05 #4
Thanks for your insight everyone. I think my use of Task Manager to
determine the memory leak was the problem.

"deancoo" <s2*******@yaho o.ca> wrote in message
news:iTiQd.5900 0$L_3.21722@clg rps13...
I'm pretty sure that the program bellow contains a memory leak. After
deleting the "hand_vecto r" instance, not all memory is released (only
about
30%). It appears to me as though the instances of "hand" are still
hanging
around. Are they not on the heap? Shouldn't they be released along with
the
"hand_vecto r" instance? Thanks for any help.

d

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class hand {
public:
vector<int> my_ints;
};

class hand_vector {
public:
vector<hand> myhands;
};

int main(int argc, char *argv[])
{

system("PAUSE") ;

hand_vector *myvector = new hand_vector;
hand v_stub;

for (int i=1; i<=5000000; i++) {
myvector->myhands.push_b ack(v_stub);
};

system("PAUSE") ;

vector<hand>::i terator j;

for (j=myvector->myhands.begin( ); j!=myvector->myhands.end( ); j++) {
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
j->my_ints.push_b ack(0);
};

system("PAUSE") ;

delete myvector;

system("PAUSE") ;
return EXIT_SUCCESS;
}

Jul 23 '05 #5

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

Similar topics

8
3408
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
17
4798
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is easier to reproduce (e.g.: remote machine not available). After about 1 day, I get a usage of 300MB of memory. I have used .NET Memory Profiler tool to try to see where the leak is located. For all the leaky instances, I can see the following (I...
4
6078
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password"; scope.Path.Path=@"\\pc\root\cimv2";
20
8092
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();
23
4545
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
8532
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of each object within my JS app to help locate my problem? Thanks
7
6929
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports the status of the machines. Upon we follow the .NET object lifetime recommendations the application is constantly consuming more memory! The problem is on the ManagementObjectSearch, upon we Dispose the object it seems that is not releasing the...
3
5311
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?
7
15693
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a lot of memory but I still had to use it. 1. After using DLLImport and seeing the memory leak I tried to load and
22
9332
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few hundred reports there is significant memory leak. The application can consume over 1GB of memory where it...
0
8438
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, 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...
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8779
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 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...
1
8549
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,...
0
7376
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5660
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();...
0
4186
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...
1
2765
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
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.