refactor: Use aliasing shared_ptr in Sock::Wait #34471

pull maflcko wants to merge 1 commits into bitcoin:master from maflcko:2602-refactor-alias-shptr changing 1 files +5 −3
  1. maflcko commented at 6:37 PM on February 1, 2026: member

    Currently, a no-op lambda is used as the deleter for the temporary shared pointer helper in Sock::Wait. This is perfectly fine, but has a few style issues:

    Fix all issues by just using an aliasing shared pointer, which points to this, but is otherwise empty (sits on the stack without any heap allocations).

  2. DrahtBot added the label Refactoring on Feb 1, 2026
  3. DrahtBot commented at 6:38 PM on February 1, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK hodlinator, sedited, vasild

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. maflcko commented at 6:41 PM on February 1, 2026: member

    If someone wants to reproduce the GCC-16 warning on a fresh install of Fedora 43:

    dnf install -y git cmake pkgconfig boost-devel libevent-devel sqlite-devel capnproto capnproto-devel zeromq-devel ccache qt6-qttools-devel qt6-qtbase-devel qrencode-devel systemtap-sdt-devel python3-zmq libstdc++
    
    dnf -y copr enable jwakely/gcc-latest && dnf -y install gcc-latest
    
    git clone https://github.com/bitcoin/bitcoin.git  --depth=1 ./b-c && cd b-c
    
    rm -rf ./bld/ && cmake -B bld -DCMAKE_C_COMPILER="/opt/gcc-latest/bin/gcc" -DCMAKE_CXX_COMPILER="/opt/gcc-latest/bin/g++;-static-libstdc++" --preset=dev-mode
    
    cmake --build ./bld -j $(nproc)
    

    Will print:

    [ 13%] Building CXX object src/util/CMakeFiles/bitcoin_util.dir/sock.cpp.o
    In file included from /opt/gcc-latest/include/c++/16.0.1/bits/stl_iterator.h:78,
                     from /opt/gcc-latest/include/c++/16.0.1/bits/stl_algobase.h:66,
                     from /opt/gcc-latest/include/c++/16.0.1/bits/stl_algo.h:60,
                     from /opt/gcc-latest/include/c++/16.0.1/chrono:57,
                     from /b-c/src/util/time.h:9,
                     from /b-c/src/common/system.h:10,
                     from /b-c/src/util/sock.cpp:5:
    In function ‘constexpr void std::destroy_at(_Tp*) [with _Tp = mutex]’,
        inlined from ‘constexpr void std::_Destroy(_Tp*) [with _Tp = mutex]’ at /opt/gcc-latest/include/c++/16.0.1/bits/stl_construct.h:164:22,
        inlined from ‘static constexpr void std::allocator_traits<std::allocator<void> >::destroy(allocator_type&, _Up*) [with _Up = std::mutex]’ at /opt/gcc-latest/include/c++/16.0.1/bits/alloc_traits.h:819:17,
        inlined from ‘void std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_M_dispose() [with _Tp = std::mutex; _Alloc = std::allocator<void>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:675:35,
        inlined from ‘void std::_Sp_counted_base<_Lp>::_M_release() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:423:18,
        inlined from ‘void std::_Sp_counted_base<_Lp>::_M_release() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:392:5,
        inlined from ‘std::__shared_count<_Lp>::~__shared_count() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1129:21,
        inlined from ‘std::__shared_ptr<_Tp, _Lp>::~__shared_ptr() [with _Tp = const Sock; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1603:7,
        inlined from ‘std::shared_ptr<const Sock>::~shared_ptr()’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr.h:175:11,
        inlined from ‘virtual bool Sock::Wait(std::chrono::milliseconds, Event, Event*) const’ at /b-c/src/util/sock.cpp:157:1:
    /opt/gcc-latest/include/c++/16.0.1/bits/stl_construct.h:88:9: warning: array subscript ‘std::mutex[0]’ is partly outside array bounds of ‘unsigned char [24]’ [-Warray-bounds=]
       88 |         __location->~_Tp();
          |         ^~~~~~~~~~
    In file included from /opt/gcc-latest/include/c++/16.0.1/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                     from /opt/gcc-latest/include/c++/16.0.1/bits/allocator.h:46,
                     from /opt/gcc-latest/include/c++/16.0.1/string:46,
                     from /opt/gcc-latest/include/c++/16.0.1/bits/locale_classes.h:43,
                     from /opt/gcc-latest/include/c++/16.0.1/bits/ios_base.h:43,
                     from /opt/gcc-latest/include/c++/16.0.1/ios:46,
                     from /opt/gcc-latest/include/c++/16.0.1/istream:42,
                     from /opt/gcc-latest/include/c++/16.0.1/sstream:42,
                     from /opt/gcc-latest/include/c++/16.0.1/chrono:61:
    In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*) [with _Tp = std::_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, std::allocator<void>, __gnu_cxx::_S_atomic>]’,
        inlined from ‘constexpr _Tp* std::allocator< <template-parameter-1-1> >::allocate(std::size_t) [with _Tp = std::_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, std::allocator<void>, __gnu_cxx::_S_atomic>]’ at /opt/gcc-latest/include/c++/16.0.1/bits/allocator.h:203:40,
        inlined from ‘static constexpr _Tp* std::allocator_traits<std::allocator<_CharT> >::allocate(allocator_type&, size_type) [with _Tp = std::_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, std::allocator<void>, __gnu_cxx::_S_atomic>]’ at /opt/gcc-latest/include/c++/16.0.1/bits/alloc_traits.h:614:28,
        inlined from ‘std::__allocated_ptr<_Alloc> std::__allocate_guarded(_Alloc&) [with _Alloc = allocator<_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, allocator<void>, __gnu_cxx::_S_atomic> >]’ at /opt/gcc-latest/include/c++/16.0.1/bits/allocated_ptr.h:103:69,
        inlined from ‘std::__shared_count<_Lp>::__shared_count(_Ptr, _Deleter, _Alloc) [with _Ptr = const Sock*; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; _Alloc = std::allocator<void>; <template-parameter-2-4> = void; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1007:13,
        inlined from ‘std::__shared_count<_Lp>::__shared_count(_Ptr, _Deleter) [with _Ptr = const Sock*; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; <template-parameter-2-3> = void; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:996:57,
        inlined from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Yp*, _Deleter) [with _Yp = const Sock; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; <template-parameter-2-3> = void; _Tp = const Sock; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1557:17,
        inlined from ‘std::shared_ptr<_Tp>::shared_ptr(_Yp*, _Deleter) [with _Yp = const Sock; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; <template-parameter-2-3> = void; _Tp = const Sock]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr.h:231:48,
        inlined from ‘virtual bool Sock::Wait(std::chrono::milliseconds, Event, Event*) const’ at /b-c/src/util/sock.cpp:144:64:
    /opt/gcc-latest/include/c++/16.0.1/bits/new_allocator.h:151:73: note: at offset 16 into object of size 24 allocated by ‘operator new’
      151 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
          |                                                                         ^
    In file included from /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr.h:53,
                     from /opt/gcc-latest/include/c++/16.0.1/chrono:64:
    In destructor ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::~_Sp_counted_ptr_inplace() [with _Tp = std::mutex; _Alloc = std::allocator<void>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’,
        inlined from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::~_Sp_counted_ptr_inplace() [with _Tp = std::mutex; _Alloc = std::allocator<void>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:670:45,
        inlined from ‘void std::_Sp_counted_base<_Lp>::_M_destroy() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:143:9,
        inlined from ‘void std::_Sp_counted_base<_Lp>::_M_release() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:424:18,
        inlined from ‘void std::_Sp_counted_base<_Lp>::_M_release() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:392:5,
        inlined from ‘std::__shared_count<_Lp>::~__shared_count() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1129:21,
        inlined from ‘std::__shared_ptr<_Tp, _Lp>::~__shared_ptr() [with _Tp = const Sock; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1603:7,
        inlined from ‘std::shared_ptr<const Sock>::~shared_ptr()’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr.h:175:11,
        inlined from ‘virtual bool Sock::Wait(std::chrono::milliseconds, Event, Event*) const’ at /b-c/src/util/sock.cpp:157:1:
    /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:670:45: warning: array subscript ‘std::_Sp_counted_ptr_inplace<std::mutex, std::allocator<void>, __gnu_cxx::_S_atomic>[0]’ is partly outside array bounds of ‘unsigned char [24]’ [-Warray-bounds=]
      670 |       ~_Sp_counted_ptr_inplace() noexcept { }
          |                                             ^
    In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*) [with _Tp = std::_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, std::allocator<void>, __gnu_cxx::_S_atomic>]’,
        inlined from ‘constexpr _Tp* std::allocator< <template-parameter-1-1> >::allocate(std::size_t) [with _Tp = std::_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, std::allocator<void>, __gnu_cxx::_S_atomic>]’ at /opt/gcc-latest/include/c++/16.0.1/bits/allocator.h:203:40,
        inlined from ‘static constexpr _Tp* std::allocator_traits<std::allocator<_CharT> >::allocate(allocator_type&, size_type) [with _Tp = std::_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, std::allocator<void>, __gnu_cxx::_S_atomic>]’ at /opt/gcc-latest/include/c++/16.0.1/bits/alloc_traits.h:614:28,
        inlined from ‘std::__allocated_ptr<_Alloc> std::__allocate_guarded(_Alloc&) [with _Alloc = allocator<_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, allocator<void>, __gnu_cxx::_S_atomic> >]’ at /opt/gcc-latest/include/c++/16.0.1/bits/allocated_ptr.h:103:69,
        inlined from ‘std::__shared_count<_Lp>::__shared_count(_Ptr, _Deleter, _Alloc) [with _Ptr = const Sock*; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; _Alloc = std::allocator<void>; <template-parameter-2-4> = void; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1007:13,
        inlined from ‘std::__shared_count<_Lp>::__shared_count(_Ptr, _Deleter) [with _Ptr = const Sock*; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; <template-parameter-2-3> = void; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:996:57,
        inlined from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Yp*, _Deleter) [with _Yp = const Sock; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; <template-parameter-2-3> = void; _Tp = const Sock; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1557:17,
        inlined from ‘std::shared_ptr<_Tp>::shared_ptr(_Yp*, _Deleter) [with _Yp = const Sock; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; <template-parameter-2-3> = void; _Tp = const Sock]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr.h:231:48,
        inlined from ‘virtual bool Sock::Wait(std::chrono::milliseconds, Event, Event*) const’ at /b-c/src/util/sock.cpp:144:64:
    /opt/gcc-latest/include/c++/16.0.1/bits/new_allocator.h:151:73: note: object of size 24 allocated by ‘operator new’
      151 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
          |                                                                         ^
    In destructor ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::~_Sp_counted_ptr_inplace() [with _Tp = std::mutex; _Alloc = std::allocator<void>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’,
        inlined from ‘void std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_M_destroy() [with _Tp = std::mutex; _Alloc = std::allocator<void>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:684:32,
        inlined from ‘void std::_Sp_counted_base<_Lp>::_M_release() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:424:18,
        inlined from ‘void std::_Sp_counted_base<_Lp>::_M_release() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:392:5,
        inlined from ‘std::__shared_count<_Lp>::~__shared_count() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1129:21,
        inlined from ‘std::__shared_ptr<_Tp, _Lp>::~__shared_ptr() [with _Tp = const Sock; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1603:7,
        inlined from ‘std::shared_ptr<const Sock>::~shared_ptr()’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr.h:175:11,
        inlined from ‘virtual bool Sock::Wait(std::chrono::milliseconds, Event, Event*) const’ at /b-c/src/util/sock.cpp:157:1:
    /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:670:45: warning: array subscript ‘std::_Sp_counted_ptr_inplace<std::mutex, std::allocator<void>, __gnu_cxx::_S_atomic>[0]’ is partly outside array bounds of ‘unsigned char [24]’ [-Warray-bounds=]
      670 |       ~_Sp_counted_ptr_inplace() noexcept { }
          |                                             ^
    In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*) [with _Tp = std::_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, std::allocator<void>, __gnu_cxx::_S_atomic>]’,
        inlined from ‘constexpr _Tp* std::allocator< <template-parameter-1-1> >::allocate(std::size_t) [with _Tp = std::_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, std::allocator<void>, __gnu_cxx::_S_atomic>]’ at /opt/gcc-latest/include/c++/16.0.1/bits/allocator.h:203:40,
        inlined from ‘static constexpr _Tp* std::allocator_traits<std::allocator<_CharT> >::allocate(allocator_type&, size_type) [with _Tp = std::_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, std::allocator<void>, __gnu_cxx::_S_atomic>]’ at /opt/gcc-latest/include/c++/16.0.1/bits/alloc_traits.h:614:28,
        inlined from ‘std::__allocated_ptr<_Alloc> std::__allocate_guarded(_Alloc&) [with _Alloc = allocator<_Sp_counted_deleter<const Sock*, Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>, allocator<void>, __gnu_cxx::_S_atomic> >]’ at /opt/gcc-latest/include/c++/16.0.1/bits/allocated_ptr.h:103:69,
        inlined from ‘std::__shared_count<_Lp>::__shared_count(_Ptr, _Deleter, _Alloc) [with _Ptr = const Sock*; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; _Alloc = std::allocator<void>; <template-parameter-2-4> = void; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1007:13,
        inlined from ‘std::__shared_count<_Lp>::__shared_count(_Ptr, _Deleter) [with _Ptr = const Sock*; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; <template-parameter-2-3> = void; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:996:57,
        inlined from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Yp*, _Deleter) [with _Yp = const Sock; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; <template-parameter-2-3> = void; _Tp = const Sock; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr_base.h:1557:17,
        inlined from ‘std::shared_ptr<_Tp>::shared_ptr(_Yp*, _Deleter) [with _Yp = const Sock; _Deleter = Sock::Wait(std::chrono::milliseconds, Event, Event*) const::<lambda(const Sock*)>; <template-parameter-2-3> = void; _Tp = const Sock]’ at /opt/gcc-latest/include/c++/16.0.1/bits/shared_ptr.h:231:48,
        inlined from ‘virtual bool Sock::Wait(std::chrono::milliseconds, Event, Event*) const’ at /b-c/src/util/sock.cpp:144:64:
    /opt/gcc-latest/include/c++/16.0.1/bits/new_allocator.h:151:73: note: object of size 24 allocated by ‘operator new’
      151 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
          |                                                                         ^
    [ 13%] Linking CXX static library ../../lib/libbitcoin_util.a
    

    (This pull fixes it)

  5. refactor: Use aliasing shared_ptr in Sock::Wait faa016af54
  6. maflcko force-pushed on Feb 3, 2026
  7. hodlinator approved
  8. hodlinator commented at 1:17 PM on February 10, 2026: contributor

    ACK faa016af542763492a63de8cc972f8f4b52a58cd

    Uses shared_ptr's aliasing ctor^0 to have the local shared variable to take over the any heap-allocated refcount, which can even be nullptr for default-initialized shared_ptrs. (Example usage of l-value overload of aliasing ctor: https://stackoverflow.com/a/27109774).

    (The custom deleter on master would need to live on the heap to outlive short-lived shared_ptr instances. The refcount and custom deleter would probably be allocated together, but specializations of shared_ptr without the custom deleter could have a smaller allocation).

    Edit: Slight correction after #34471 (comment)

  9. maflcko commented at 1:46 PM on February 10, 2026: member

    Uses shared_ptr's aliasing ctor to have the local shared variable to take over the heap-allocated refcount (https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr.html) . (Example usage of l-value overload of aliasing ctor: https://stackoverflow.com/a/27109774).

    Just for reference, and not important, but in the specific case of the code in this pull request, there is no need for any heap-allocation at all. It is just two nullptr on the stack (for the empty ptr), and still two pointers on the stack after passing to the aliasing ctor. At least in libcxx: https://github.com/llvm/llvm-project/blob/2f0400c1a15b7b1ff401cc12902f7de5856cf1f7/libcxx/include/__memory/shared_ptr.h#L321-L328 and https://github.com/llvm/llvm-project/blob/2f0400c1a15b7b1ff401cc12902f7de5856cf1f7/libcxx/include/__memory/shared_ptr.h#L426-L443

  10. maflcko commented at 1:55 PM on February 10, 2026: member

    cc @vasild (author of the original code, may be qualified to ack/nack this)

  11. maflcko requested review from theuni on Feb 18, 2026
  12. sedited approved
  13. sedited commented at 12:49 PM on March 8, 2026: contributor

    ACK faa016af542763492a63de8cc972f8f4b52a58cd

  14. vasild approved
  15. vasild commented at 9:21 AM on March 9, 2026: contributor

    ACK faa016af542763492a63de8cc972f8f4b52a58cd

    The code is equivalent and silencing the GCC (false) warning seems enough of justification.

    From the OP:

    The lambda needs to be allocated on the heap

    Hmm, why? My understanding is that the lambda is just a temporary object on the stack, like any other object, e.g. Foo(1, 2, MyClass{5, 6}) (the temporary of MyClass lives on the stack).

    For reference: https://www.elbeno.com/blog/?p=1068 https://stackoverflow.com/questions/12202656/c11-lambda-implementation-and-memory-model

  16. fanquake merged this on Mar 9, 2026
  17. fanquake closed this on Mar 9, 2026

  18. maflcko commented at 8:15 AM on March 10, 2026: member

    From the OP:

    The lambda needs to be allocated on the heap

    Hmm, why? My understanding is that the lambda is just a temporary object on the stack, like any other object, e.g. Foo(1, 2, MyClass{5, 6}) (the temporary of MyClass lives on the stack).

    The lambda is stored in the control block, which is heap-allocated.

  19. maflcko deleted the branch on Mar 10, 2026


theuni


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-22 06:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me