I have std::vector< tr1::shared_ptr<mapObject> >one that I am trying to create from the data contained in a compressed file. Here is the function I'm trying to do with:
using std::tr1::shared_ptr;
template<typename T>
void loadSharedPtrVector(std::vector< shared_ptr<T> >& vect, TCODZip& zip)
{
vect.clear();
int numT = zip.getInt();
for(int i=0; i<numT; ++i)
{
int type = zip.getInt();
shared_ptr<T> Tptr(new T);
T newT = T::loadType(type, zip);
Tptr.reset(&newT);
std::cerr << "load: " << Tptr->getPosition() << std::endl;
vect.push_back(Tptr);
}
for(int i=0; i<numT; ++i)
{
std::cerr << "loadDone: " << vect[i]->getPosition() << std::endl;
}
}
The above function is called by this bit of code here:
typedef std::tr1::shared_ptr<mapObject> featurePtr;
utility::loadSharedPtrVector<mapObject>(features, zip);
vector<featurePtr>::const_iterator fit;
for(fit=features.begin(); fit<features.end(); ++fit)
{
cerr << "afterCall: " << (*fit)->getPosition() << endl;
}
When this is done, the operators cerrgive this result (each set of output files has ~ 50 lines, so I cut most for short):
load: (5,40)
load: (5,45)
load: (5,58)
(etc. all 'load' lines are correct output)
load: (87,68)
load: (11,5)
loadDone: (11,5)
loadDone: (11,5)
loadDone: (11,5)
loadDone: (11,5)
loadDone: (11,5)
loadDone: (11,5)
(etc. all 'loadDone' lines are the same)
afterCall: (11,5)
afterCall: (10,1)
afterCall: (10,1)
afterCall: (10,1)
afterCall: (10,1)
afterCall: (10,1)
afterCall: (10,1)
(etc. all 'afterCall' lines are the same except for the first)
I apparently have some misconceptions regarding how shared_ptrs work. I realized that I was pushing copies Tptrto vect, and therefore all of its indices are the same, although I thought that declaring a new shared_ptr in a loop would make a separate pointer from others already in vect, but I think not.
, "afterCall" "loadDone" ( ). (10,1) (2274756,134747232) (134747232, 16), (10,1) , .
, shared_ptr. - , ? , , .