The object of which a shared pointer shares ownership is deleted when there are no more shared pointers sharing ownership, e.g. typically in the destructor of some shared pointer (but also in an assignment, or upon explicit reset).
(Note that there may be shared pointers of many different types sharing ownership of the same object!)
That said, your code has issues. Perhaps it might be better like this:
// Requirement: C must be convertible to CCob
template <typename C>
C * CLink::createCob()
{
auto p = std::make_shared<C>();
if (p->init(this) != OK) { return nullptr; }
mCobs.push_back(std::move(p));
return mCobs.back().get();
}
The usage would then be: link.createCob<CobOfSomeTypo>()
. It depends, though, on whether you need to be able to take ownership of existing pointers. That itself would be something worth fixing, though.
It's also possible (but impossible to tell from your question) that you dont actually need shared pointers at all and could simply work with unique pointers.