C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Save Cascading Free of Smart Pointers

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Tue, 16 Jul 2024 19:36:46 -0400
On Tue, Jul 16, 2024 at 5:14 PM M.C.A. (Marco) Devillers via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> I came up against this and wanted to float an idea. It doesn't seem
> popular but I think I am right. This should be resolved.
>
> Document number: xxx
> Date: 2024-7-16
> Audience: GCC email list
> Reply-to: marco.devillers_at_[hidden], std-proposals_at_[hidden]
>
> I.
> II. Introduction
>
> Because C++ smart pointers are based on RAII it is easy to trigger an
> overflow of the C stack since destructors call each other. Smart
> pointers are supposed to be safe, smart pointers are likely to be used
> extensively in the future, and this behaviour could make a large
> number of C++ programs core dump unexpectedly.
> This proposal is to remove this behaviour from GCCs standard library
> and also showcases a small trick by which that can be done.

This small trick does not "fix" the "problem".

Your `safe_unique_ptr` works, but if someone writes a `list_node` type
that manages the memory directly through its destructor (instead of
relegating it to `unique_ptr`), then that type has the same problem.
The problem exists for every RAII type and would have to be solved
*independently* for every RAII type.

It also doesn't seem to work across different types. Like, if
`list_node` held a `unique_ptr<T>` whose `T` holds a `shared_ptr<U>`,
this cascade doesn't work.

Even ignoring the other deficiencies you've pointed out, it's a
solution that just doesn't work.

Received on 2024-07-16 23:36:58