Question

I use a boost dispatcher (io_service) to execute asynchronously "methodB". Into this method, I would like to keep a pointer to the instance of the class B, so I use shared_ptr. But in the below example, I wonder whether after the scope of "methodA", the pointer will still be accessible into "methodB" or if the pointer refcounter will be equaled to zero.

void ClassA::methodA(){
  shared_ptr<ClassB> pointer(new ClassB);
  m_dispatcher.post(boost::bind(&ClassA::methodB, this, pointer); // boost io_service
}

void ClassA::methodB(shared_ptr<ClassB> pointer){
  ClassB *p = pointer.get(); // access to pointer ???
}

Thanks you very much.

Was it helpful?

Solution

using boost::bind in this fashion will copy the arguments ensuring shared_ptr<ClassB> remains in scope. What you're doing is perfectly fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top