mardi 28 mars 2017

Don't want to delete object when shared_ptr get out of scope

I have a situation that I have some code like this. We need to add child objects to a clean up list, which will delete these children later when the class get destroyed. But we wish to use set for internal safety reasons, and obviously the set has longer lifetime.

//This is will delete twice, if this is called later.
~classX(){
    cleanList.clear();
}

classX::addToCleanList(T* child){
    set<shared_ptr<T> > cleanList;

    shared_ptr<T> p = shared_ptr(child);

    //Set has a customized comparetor
    //If the set already has the object pointer, insert will fail 
    //and the shared_ptr will get out of the scope 
    set.insert(p);
}
//When it gets out of scope, p will be destroyed.

This will cause issue, since the child objects are still needed.

Any solution to this? I don't see unique_ptr or shared_ptr and move semantic can avoid being deleted when they get out of the scope.

Aucun commentaire:

Enregistrer un commentaire