C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal Idea: Fallback Deterministic Reference Counting for Unprovable Lifetime Profiles

From: vxadminMx <vxadminMx_at_[hidden]>
Date: Fri, 04 Sep 2026 08:44:49 +0000
To clarify,

This is absolutely not a replacement for smart pointers, nor does it penalize clean code. If a local variable's lifetime can be mathematically proven safe by the static analyzer, this mechanism compiles down to absolute zero runtime overhead.

The primary goal here is ergonomic migration. Forcing developers to completely refactor massive legacy API signatures to use explicit smart pointer wrappers is a barrier to C++ safety adoption. This provides a transparent, compiler-managed fallback layer so legacy code can compile safely today, while developers incrementally refine their code to satisfy static proofs tomorrow.

On Friday, 4 September 2026 at 13:27, vxadminMx <vxadminMx_at_[hidden]> wrote:

> Hi Sebastian,
>
> Thank you for these excellent questions! You've highlighted the exact engineering boundaries I am actively mapping out. I actually just finished building a raw runtime crucible to stress-test these constraints under extreme out-of-order lifecycle drift.
>
> To clarify how this behaves in practice based on those telemetry findings:
>
> 1. Object Size and Layout:
> The layout and sizeof(T) of the underlying type remain completely untouched. Changing object layouts conditionally based on local static analysis would break ABI stability across translation units.
>
> Instead, the "implicit indirection" applies exclusively to the storage or reference-handling context within the unprovable scope. The compiler transparently handles this at the allocation site (wrapping it in a fat-pointer configuration or dynamic tracking frame), while downstream functions still receive a standard, layout-compatible raw pointer or reference.
>
> 2. Scope of Application:
> This mechanism is strictly intended for automatic (stack/local) variables whose escape paths out of the current scope cannot be mathematically guaranteed clean by a compile-time borrow checker. It does not alter class member structures or base classes, avoiding implementation leaks across translation unit boundaries.
>
> 3. Thread Safety & Cycle Detection:
> The tracking relies on atomic operations to ensure safety across concurrent boundaries (e.g., an unprovable lambda capture passed to a thread pool).
>
> Regarding cycles: the runtime pass handles cycle hazards by actively monitoring the control-block topology. In recent stress testing, the engine successfully intercepted real-time retain loops and broke the edge deterministicly before leaking. For unprovable graphs where a cycle cannot be safely resolved at runtime, the compiler would fall back to a hard compilation error rather than risk a silent leak.
>
> 4. Asymmetric Lifecycles & Why Optimizers Aren't Enough:
> While an optimizer can sometimes elide explicit smart pointer allocations via escape analysis, it requires the developer to first completely refactor their API signatures to use smart pointers.
>
> In a recent stress test of this fallback architecture, I dropped 1,000,000 parent nodes while keeping an internal "ghost node" alive out-of-order. The underlying memory runtime successfully isolated the drift, keeping the inner instance fully intact without a use-after-free crash or a spinlock deadlock.
>
> The goal of this proposal is to provide this exact safety net for legacy C++ or unannotated interfaces, using dynamic reference counting purely as a compiler-inserted fallback where static safety proofs fall short.
>
> On Friday, 4 September 2026 at 11:27, Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
>> How could that work in practice?
>>
>> The size of those objects would not be fixed? As only sometimes - depending on the analysis - a reference counter is attached.
>>
>> Is it for stack (automatic/local/scoped) variables? Or also for member variables or base classes?
>>
>> Is it for safety within a thread or even safety between different threads?
>>
>> Does the code need special annotations or classes/templates at all? If the analysis proves that e.g. std::shared_ptr is never needed for a variable, it can be optimized away under the as-if rule. Would a better optimizer that can do that be enough?
>>
>>> -----Ursprüngliche Nachricht-----
>>> Von: vxadminMx via Std-Proposals <std-proposals_at_[hidden]>
>>> Gesendet: Fr 04.09.2026 06:59
>>> Betreff: [std-proposals] Proposal Idea: Fallback Deterministic Reference Counting for Unprovable Lifetime Profiles
>>> An: std-proposals_at_[hidden];
>>> CC: vxadminMx <vxadminMx_at_[hidden]>;
>>>
>>> Hi everyone,
>>> I want to gather early feedback on an opt-in language mechanism that introduces a hybrid safety net within the upcoming C++ Safety Profiles framework.
>>> Currently, static lifetime analysis models force a strict binary constraint: code must either mathematically prove safety at compile time or fail compilation completely. This breaks interface ergonomics and blocks easy legacy C++ migration paths.
>>> I am drafting a proposal for a dynamic safety degradation mechanism. Within a translation unit or scope marked with a profile attribute (e.g., [[carries_profile(safety)]]), if local variables or control flow paths cannot be statically verified clean by a borrow checker, the implementation transparently injects deterministic, RAII-based atomic reference counting control blocks at the failure site rather than throwing a hard compilation error.
>>> This ensures complete memory safety at runtime for complex topologies or legacy code paths without forcing the developer to invasively change API layouts to use explicit smart pointer wrappers (std::shared_ptr).
>>> Key areas I'm trying to address in the draft include:
>>>
>>> - Keeping type layouts untouched to avoid ODR/ABI breaks across translation units (using implicit outer pointer indirection only where static proofs fail).
>>> - Intercepting potential reference cycles during escape-path analysis.
>>> - Managing cascade deallocation latency by optionally deferring cleanup tasks via execution profiles.
>>> I am finalizing a formal draft plugin via a custom Clang pass to prototype this natively. Before requesting an official P-number document tracker, I would love to hear thoughts on this approach, existing overlaps, or immediate edge cases the evolution groups might object to.
>>>
>>> -mxreal64
>>>
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> -mxreal64

-mxreal64

Received on 2026-09-04 08:45:02