mercredi 28 juin 2017

std::make_shared(), std::weak_ptr and cyclic references

My question is about this claim:

If any std::weak_ptr references the control block created by std::make_shared after the lifetime of all shared owners ended, the memory occupied by T persists until all weak owners get destroyed as well, which may be undesirable if sizeof(T) is large. Source

I read here, that this object live until last weak_ptr is present. Does it free object made with make_shared, with cyclic reference to self or it will live in memory forever?

For example:

struct A
{
    std::weak_ptr<A> parent;
}

void fn()
{
    auto a=std::make_shared<A>();
    a->parent = a;
} // Will it destroy here or not?

Aucun commentaire:

Enregistrer un commentaire