You mentioned in the comments that you don't create a shared_ptr
before you call shared_from_this()
. But that's the problem. x.shared_from_this()
only works once a shared_ptr
to x
exists. If you never create one, you're violating a precondition of shared_from_this()
, so your program has undefined behaviour.
shared_from_this()
only serves to retrieve a pointer sharing ownership with existing shared pointers; it cannot be used to create the first one.