C++ Logo


Advanced search

Re: [SG14] (SC22WG14.16281) Rough notes from SC22 WG21 SG14 meeting on pointer lifetime-end zap

From: Herb Sutter <hsutter_at_[hidden]>
Date: Sat, 20 Apr 2019 14:19:16 +0000
> The recent paper (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2369.pdf)

It's not just equality testing that is problematic. From the paper (emphasis added):

- A number of long-standing and heavily used concurrent algorithms, a few of which are presented in the following
- sections, involve loading, storing, CASTING, and comparing pointers to objects which might have reached their lifetime
- end between the pointer being loaded and when it is stored, reloaded, CASTED, and compared, due to concurrent
- removal and freeing of the pointed-to object

Two of the examples in the paper mention weak pointers. Here's a third that's not very well-known:

In C++, just doing a standard pointer conversion (up-cast) from Derived* to Base* can crash.

The conversion is statically known to be type-safe, but in the presence of virtual inheritance it needs to consult the Derived object's vtable to determine the correct offset adjustment to get to the Base subobject, which requires implicitly dereferencing the Derived* to read the vpointer. This comes up in std::weak_ptr's converting constructor, so implementers have to be careful how they code it. In that case, the simplest fix is to .lock().get() to ensure we have a pointer to a valid object (or null) before up-casting its value.


Received on 2019-04-20 09:21:16