Your issue is with how you are creating the self pointer:
this->self = std::shared_ptr<test>(this);
When a shared_ptr
is created with this constructor, according to the documentation,
When T is not an array type, constructs a shared_ptr that owns the pointer p. ... p must be a pointer to an object that was allocated via a C++ new expression or be 0
So the issue is that the shared_ptr
is taking ownership of your stack object, so when the object gets destructed (and the shared_ptr
along with it), shared_ptr
is trying to delete
your object that is on the stack. This is not valid.
For your use case, if you expect test
s to outlive your vector
, then you might be able to just store this
.