Memory leak...

Discussion in 'C++' started by deancoo, Feb 15, 2005.

  1. deancoo

    deancoo Guest

    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;
    }
    deancoo, Feb 15, 2005
    #1
    1. Advertising

  2. deancoo

    msalters Guest

    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
    msalters, Feb 15, 2005
    #2
    1. Advertising

  3. "deancoo" <> 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;
    > }
    >
    >
    >
    >
    Peter Koch Larsen, Feb 15, 2005
    #3
  4. deancoo

    SnaiL Guest

    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
    SnaiL, Feb 15, 2005
    #4
  5. deancoo

    deancoo Guest

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

    "deancoo" <> 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;
    > }
    >
    >
    >
    >
    deancoo, Feb 16, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Y3liZXJzdHJpa2U=?=

    datagrid memory leak?

    =?Utf-8?B?Y3liZXJzdHJpa2U=?=, Jan 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    465
    =?Utf-8?B?Y3liZXJzdHJpa2U=?=
    Jan 3, 2005
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    699
    Eric Sosman
    Mar 22, 2005
  3. Richard Heathfield

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Replies:
    4
    Views:
    351
    Richard Heathfield
    Jul 10, 2006
  4. cham
    Replies:
    5
    Views:
    768
  5. Mark Probert
    Replies:
    4
    Views:
    326
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page