Click to See Complete Forum and Search --> : STL deque question


xargon
April 10th, 2003, 10:07 AM
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<Wave *> _wavePool;
std::deque<Wave *>::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.

Xargon

Andreas Masur
April 10th, 2003, 10:20 AM
In case of having any container filled with pointers, the container only destroys its copy of the pointer itself but not the object it is pointing to. You have to do this manually...thus

std::deque<Wave *> _wavePool;
std::deque<Wave *>::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();

...

// First delete object
delete *it;

// Then delete it from container
_wavePool.erase(it);

xargon
April 10th, 2003, 10:29 AM
Ahhhhhh, I get it now. Thanks man!

Cheers,

Xargon