I'm aware that using smart pointers like std::shared_ptr
adds garbage collection when the pointer goes out of scope, but I'm unclear as to whether the garbage collection also works if I call make_shared()
multiple times on one shared_ptr
.
For example:
std::shared_ptr<MyClass> mcPtr;
void assignment(int i)
{
mcPtr = std::make_shared<MyClass>(i);
}
void main()
{
assignment(5);
// Some time later
assignment(10); // Does this cause a memory leak?
}