#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
using namespace std;
using namespace boost;
struct A {
~A() { cout << "deleted " << (void*)this << endl; }
};
int main() {
ptr_vector<A> v;
v.push_back(new A);
A *temp = &v.front();
v.release(v.begin());
delete temp;
return 0;
}
outputs:
deleted 0x300300 deleted 0x300300 c(6832) malloc: *** error for object 0x300300: double free
ptr_vector<A>::releasereturns aptr_vector<A>::auto_type, which is a kind of light-weight smart pointer in that when anauto_typeitem goes out of scope, the thing it points to is automatically deleted. To recover a raw pointer to the thing, and keep it from being deleted by theauto_ptrthat’s holding it, you need to callreleaseon that too:The first
releasetells theptr_vectorto give it up; the second tells theauto_ptrto give it up too.