On Tuesday 28 July 2015 17:18:22 Ville Voutilainen wrote:
> Right, it probably causes no chaos, but you're suggesting that
> shared_ptr checks whether an enable_shared_from_this of its target
> object has a null weak_ptr in it, and if so, assigns itself to it?
> I suppose that works as far as refcounts go, and I can't immediately
> see any chaos in it, it's
> merely an additional check that's meant to protect from cases that are
> not supposed to happen.
There's a race here, as the weak_ptr may have been shared to multiple threads,
which may be simultaneously trying to create a shared_ptr.
It's not impossible to do it, but it needs to be done carefully.
Another thing to think about is when the enable_shared_from_this comes from a
different side of the hierarchy:
struct Base {
virtual ~Base() = default;
};
struct Base2 : enable_shared_from_this<Derived> {
virtual ~Base2() = default;
};
struct Derived : Base, Base2 {
};
Does it still apply?
--
Thiago Macieira - thiago (AT)
macieira.info - thiago (AT)
kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358