1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

deque memory leak

Discussion in 'C++' started by pollypocket4eva, Sep 2, 2010.

  1. pollypocket4eva

    pollypocket4eva New Member

    Joined:
    Sep 2, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I can't figure out why the following is leaking memory - when I run the application the memory usage just keeps going up. Anyone see something wrong with this code:

    I'm adding some initial values to the 2d deque like so:

    Code:
    deque dq;
    for(int i = 0; i < 10; i++) {
            dq.push_back(deque());
            for(int j = 0; j < 10; j++) {
                    dq[i].push_back(new MyType(1,2));
            }
    }
    
    And then during the running of the application, the user periodically performs actions that invoke this:

    Code:
    for(int j = 0; j < 10; j++) {
        delete dq.back()[j];
    }
    dq.pop_back();
                            
    dq.push_front(deque());
    for(int j = 9; j >= 0; j--) {
            dq.front().push_front(new MyType(1,2));
    }
    
    or this:

    Code:
    for(int i = 0; i < 10; i++) {
        dq[i].push_front(new MyType(1,2));
        delete dq[i].back();
        dq[i].pop_back();
    }
    
    Much thanks for any help!
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    I see that you are doing manual memory management. The idea behind what I am saying is simply: do not do manual memory management. Storing the objects by value accomplishes that, as does using a smart pointer like std::tr1::shared_ptr. If you have access to Boost, another possibility is to use a boost::ptr_deque.
     

Share This Page