Memory leak...

D

deancoo

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;
}
 
M

msalters

deancoo said:
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
 
P

Peter Koch Larsen

deancoo said:
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;
}
 
S

SnaiL

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
 
D

deancoo

Thanks for your insight everyone. I think my use of Task Manager to
determine the memory leak was the problem.
 

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

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top