Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Memory leak...

Reply
Thread Tools

Memory leak...

 
 
deancoo
Guest
Posts: n/a
 
      02-15-2005
I'm pretty sure that the program bellow contains a memory leak. After
deleting the "hand_vector" 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_vector" 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_back(v_stub);
};

system("PAUSE");

vector<hand>::iterator j;

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

system("PAUSE");

delete myvector;

system("PAUSE");
return EXIT_SUCCESS;
}




 
Reply With Quote
 
 
 
 
msalters
Guest
Posts: n/a
 
      02-15-2005

deancoo wrote:
> I'm pretty sure that the program bellow contains a memory leak.

After
> deleting the "hand_vector" 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_vector" 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

 
Reply With Quote
 
 
 
 
Peter Koch Larsen
Guest
Posts: n/a
 
      02-15-2005

"deancoo" <(E-Mail Removed)> skrev i en meddelelse
news:iTiQd.59000$L_3.21722@clgrps13...
> I'm pretty sure that the program bellow contains a memory leak. After
> deleting the "hand_vector" 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_vector" 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_back(v_stub);
> };
>
> system("PAUSE");
>
> vector<hand>::iterator j;
>
> for (j=myvector->myhands.begin(); j!=myvector->myhands.end(); j++) {
> j->my_ints.push_back(0);
> j->my_ints.push_back(0);
> j->my_ints.push_back(0);
> j->my_ints.push_back(0);
> j->my_ints.push_back(0);
> };
>
> system("PAUSE");
>
> delete myvector;
>
> system("PAUSE");
> return EXIT_SUCCESS;
> }
>
>
>
>



 
Reply With Quote
 
SnaiL
Guest
Posts: n/a
 
      02-15-2005
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

 
Reply With Quote
 
deancoo
Guest
Posts: n/a
 
      02-16-2005
Thanks for your insight everyone. I think my use of Task Manager to
determine the memory leak was the problem.

"deancoo" <(E-Mail Removed)> wrote in message
news:iTiQd.59000$L_3.21722@clgrps13...
> I'm pretty sure that the program bellow contains a memory leak. After
> deleting the "hand_vector" 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_vector" 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_back(v_stub);
> };
>
> system("PAUSE");
>
> vector<hand>::iterator j;
>
> for (j=myvector->myhands.begin(); j!=myvector->myhands.end(); j++) {
> j->my_ints.push_back(0);
> j->my_ints.push_back(0);
> j->my_ints.push_back(0);
> j->my_ints.push_back(0);
> j->my_ints.push_back(0);
> };
>
> system("PAUSE");
>
> delete myvector;
>
> system("PAUSE");
> return EXIT_SUCCESS;
> }
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Patriot Memory PDC1G5600ELK Memory Review Silverstrand Front Page News 0 09-07-2005 02:24 AM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
RAM Memory or virual memory Julián Sanz García ASP .Net 4 11-12-2004 06:25 PM
GC does not release memory...memory keeps growing!!! Mahesh Prasad ASP .Net 1 02-22-2004 08:40 AM
AspNet Process Memory Issue on Win2k Server - Peformance is fine - Memory usuage doesn't stop growing Cy Huckaba ASP .Net 1 06-26-2003 04:00 AM



Advertisments