Thread: deque memory leak

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    deque memory leak

    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<deque<MyType*> > dq;
    for(int i = 0; i < 10; i++) {
    	dq.push_back(deque<MyType*>());
    	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<MyType*>());
    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you really need a deque of deque of pointers to MyType rather than a deque of deque of MyType objects? If so, have you considered using a deque<deque<tr1::shared_ptr<MyType> > > instead?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Well, MyType contains a lot of data, so I was reluctant to pass copies. I guess I can reconsider that a bit. I hadn't considered using shared_ptr as I don't feel I have a firm grasp on what that is. How would that relate to this problem? Given your suggestion, should I take you to mean that the deque of deque of pointers to MyType is a bad way to do things?
    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pollypocket4eva
    I hadn't considered using shared_ptr as I don't feel I have a firm grasp on what that is. How would that relate to this problem? Given your suggestion, should I take you to mean that the deque of deque of pointers to MyType is a bad way to do things?
    Well, you say that you have a memory leak problem, then 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Hey,
    Thanks for your help. I've just found my problem. Turns out the deque was actually fine all along. I thought I'd ruled out all other possible causes of the leak, but there was something I'd missed.
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM