Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. STL deque question

STL deque question

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++data-structuresquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pankajdaga
    wrote on last edited by
    #1

    Hello all, I thought when I call erase() method on an STL deque, it is supposed to call the corresponding destructor of the object. However, it does not when I do this. Here is some code to illustrate the problem. std::deque _wavePool; std::deque::iterator it; Wave *wav1 = new Wave("D:\\waves\\v1.wav", true); Wave *wav2 = new Wave("D:\\waves\\think.wav", true); _wavePool.push_back(wav1); _wavePool.push_back(wav2); it = _wavePool.begin() Now, if I call... _wavePool.erase(it); The wave destructor never gets called. Am I not supposed to put heap pointers in a queue or do I have to do this manually. Thanks for any help you might give me. Pankaj Without struggle, there is no progress

    E Z A 3 Replies Last reply
    0
    • P pankajdaga

      Hello all, I thought when I call erase() method on an STL deque, it is supposed to call the corresponding destructor of the object. However, it does not when I do this. Here is some code to illustrate the problem. std::deque _wavePool; std::deque::iterator it; Wave *wav1 = new Wave("D:\\waves\\v1.wav", true); Wave *wav2 = new Wave("D:\\waves\\think.wav", true); _wavePool.push_back(wav1); _wavePool.push_back(wav2); it = _wavePool.begin() Now, if I call... _wavePool.erase(it); The wave destructor never gets called. Am I not supposed to put heap pointers in a queue or do I have to do this manually. Thanks for any help you might give me. Pankaj Without struggle, there is no progress

      E Offline
      E Offline
      Erik Juhl
      wrote on last edited by
      #2

      I think the rule is that anything you create on the heap you have to free yourself. Or you could use a deque of auto_ptr<>. I think the best advice is to grab the boost stl library[^] and use a scoped_array or something along htose lines.

      J 1 Reply Last reply
      0
      • P pankajdaga

        Hello all, I thought when I call erase() method on an STL deque, it is supposed to call the corresponding destructor of the object. However, it does not when I do this. Here is some code to illustrate the problem. std::deque _wavePool; std::deque::iterator it; Wave *wav1 = new Wave("D:\\waves\\v1.wav", true); Wave *wav2 = new Wave("D:\\waves\\think.wav", true); _wavePool.push_back(wav1); _wavePool.push_back(wav2); it = _wavePool.begin() Now, if I call... _wavePool.erase(it); The wave destructor never gets called. Am I not supposed to put heap pointers in a queue or do I have to do this manually. Thanks for any help you might give me. Pankaj Without struggle, there is no progress

        Z Offline
        Z Offline
        Zdeslav Vojkovic
        wrote on last edited by
        #3

        erase() calls destructor of contained objects, not delete. destructor of a pointer just destroys the pointer variable, not the underlying object. you have to delete allocated objects explicitly.

        1 Reply Last reply
        0
        • E Erik Juhl

          I think the rule is that anything you create on the heap you have to free yourself. Or you could use a deque of auto_ptr<>. I think the best advice is to grab the boost stl library[^] and use a scoped_array or something along htose lines.

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #4

          Erik Juhl wrote: I think the rule is that anything you create on the heap you have to free yourself. Yes, anything you create on the heap you delete yourself. You could use some type reference counting pointer for this or call the delete yourself. I do the latter more often. John

          1 Reply Last reply
          0
          • P pankajdaga

            Hello all, I thought when I call erase() method on an STL deque, it is supposed to call the corresponding destructor of the object. However, it does not when I do this. Here is some code to illustrate the problem. std::deque _wavePool; std::deque::iterator it; Wave *wav1 = new Wave("D:\\waves\\v1.wav", true); Wave *wav2 = new Wave("D:\\waves\\think.wav", true); _wavePool.push_back(wav1); _wavePool.push_back(wav2); it = _wavePool.begin() Now, if I call... _wavePool.erase(it); The wave destructor never gets called. Am I not supposed to put heap pointers in a queue or do I have to do this manually. Thanks for any help you might give me. Pankaj Without struggle, there is no progress

            A Offline
            A Offline
            Alvaro Mendez
            wrote on last edited by
            #5

            Use auto_ptr instead of the pointers directly:

            std::deque< auto_ptr<Wave> > _wavePool;
            std::deque< auto_ptr<Wave> >::iterator it;

            auto_ptr<Wave> wav1(new Wave("D:\\waves\\v1.wav", true));
            _wavePool.push_back(wav1);
            wav1->Splash();

            _wavePool.erase(it); // will destroy the auto_ptr object, thus deleting the Wave pointer.

            Regards, Alvaro


            When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups