I have a segfault occurring when trying to downcast a std::shared_ptr
using std::static_pointer_cast
, where the derived class also contains a std::weak_ptr
.
Here is a MWE:
#include<iostream>
#include<memory>
struct Base {};
struct Derived : Base
{
std::weak_ptr<int> wp;
};
int main()
{
auto pB = std::make_shared<Base>(); // Create a pointer to Base class instance
auto pD = std::static_pointer_cast<Derived>(pB); // Downcast this to a Derived class instance
auto pint = std::make_shared<int>(0); // Define a pointer to an integer
std::cout << "assigning pint" << std::endl;
pD->wp = pint; //Attempt to assign member of Derived
std::cout << "Did not segfault" << std::endl;
return 0;
}