By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Memory leak...

P: n/a
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;
}


Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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

Jul 23 '05 #2

P: n/a

"deancoo" <s2*******@yahoo.ca> 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;
}

Jul 23 '05 #3

P: n/a
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

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

"deancoo" <s2*******@yahoo.ca> 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;
}

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.