C++ Logo

SG14

Advanced search

Subject: Re: (SC22WG14.16281) Rough notes from SC22 WG21 SG14 meeting on pointer lifetime-end zap
From: Herb Sutter (hsutter_at_[hidden])
Date: 2019-04-20 09:19:16


> 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.

Herb



SG14 list run by sg14-owner@lists.isocpp.org

Older Archives on Google Groups