STL deque question
-
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
-
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
-
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
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.
-
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.
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
-
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
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