In all answers I have seen so far, it was either that someone was using shared_from_this()
when there was indeed no shared pointer of the object, or the inheritance of enable_shared_from_this
was private.
That seems not to be the case here.
Do you see why it throws the exception?
The exception gets thrown in the instantiation (constructor) of the shared pointer for ShoeManager.
main()
{
std::shared_ptr<IVRshoeDevice> leftShoe = InitDeviceConfiguration(leftConfiguration, "leftName");
std::shared_ptr<IVRshoeDevice> rightShoe = InitDeviceConfiguration(rightConfiguration, "rightName");
std::shared_ptr<ShoeManager> shoeManager = std::make_shared<ShoeManager>(leftShoe, rightShoe);
return 0;
}
ShoeManager inherits from enable_shared_from_this
class ShoeManager : public std::enable_shared_from_this<ShoeManager>
{
public:
ShoeManager(std::shared_ptr<IVRshoeDevice> shoe1, std::shared_ptr<IVRshoeDevice> shoe2);
}
The exception is thrown on this line.
ShoeManager::ShoeManager(std::shared_ptr<IVRshoeDevice> shoe1, std::shared_ptr<Freeaim::IVRshoeDevice> shoe2)
{
shoe1->SetManager(shared_from_this()); // < bad weak_ptr exception
shoe2->SetManager(shared_from_this());
}
What I want is that both shoes know what they are managed by. In another language I would just do: shoe1->SetManager(this);
, but with pointers in cpp that is not very safe. Any options?