I have deliberately introduced a circular dependency in below code.
My doubt is that since when the shared pointer w goes out of scope, then the ref count is not zero and hence Widget object won't be destroyed.
But at the end of main, won't the shared pointer ' w->mGadget->mWidget' too go out of scope, since everything is known to cease to exist at the end of main? I am bit confused about the scope here. I was expecting that all entity scope should get over when main exits. Where is the missing link in my understanding here?
#include <memory>
#include <iostream>
struct Gadget;
struct Widget
{
std::shared_ptr<Gadget> mGadget;
};
struct Gadget
{
std::shared_ptr<Widget> mWidget;
};
int main()
{
auto w = std::make_shared<Widget>();
w->mGadget = std::make_shared<Gadget>();
w->mGadget->mWidget = w;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire