In my code, I have
void Test(){
std::shared_ptr<A> a = std::shared_ptr<A>(new A());
std::shared_ptr<B> b = std::shared_ptr<B>(new B(a->getSomething());
}
The problem is, that a
is released before b
, but during b
release I need something from a
, that was obtained via a->getSomethig()
. I cannot change getSomething
method.
Can I change (set) order of releasing shared pointers?
Assuming you reversed the order of objects in the original question (otherwise, the question doesn't make any sense), you do not need to change the order of release, instead, you should use aliasing form of shared_ptr constructor. Something like that:
std::shared_ptr<B> b(a, a->getSomething());