Fix inconsistent operator delete usages
François Dumont
frs.dumont@gmail.com
Mon Feb 4 21:16:00 GMT 2019
On 2/1/19 2:34 PM, Jonathan Wakely wrote:
> On 31/01/19 22:29 +0100, François Dumont wrote:
>> Â Â Â I was writing a test which needed to override the std::nothrow
>> versions of the operator new and delete to control
>> get_temporary_buffer behavior and noticed that it is inconsistent
>> with release_temporary_buffer in terms of new/delete operators.
>>
>> Â Â Â Grepping for other std::nothrow usages I found some others and
>> especially one in libsupc++.
>>
>> Â Â Â I don't know how serious it is considering the Standard. As long
>> as you stick to the libstdc++ operators it is fine. Only users
>> overriding those operators will notice.
>>
>> Â Â Â Tested under Linux x86_64 normal mode with some failures but none
>> related to this patch I think but of course you better check on your
>> side.
>>
>> Â Â Â * libsupc++/atexit_thread.cc (run(void*)): Call std::nothrow delete
>> Â Â Â operator.
>> Â Â Â * include/bits/stl_tempbuf.h (return_temporary_buffer): Likewise.
>> Â Â Â * include/profile/impl/profiler_trace.h
>> Â Â Â (__trace_base<>::~__trace_base()): Likewise.
>> Â Â Â (__trace_base<>::__add_object(__stack_t)): Likewise.
>> Â Â Â (__trace_base<>::__retire_object(__stack_t)): Likewise.
>>
>> Let me know if it is a go.
>
> Nope.
>
>> François
>>
>
>> diff --git a/libstdc++-v3/include/bits/stl_tempbuf.h
>> b/libstdc++-v3/include/bits/stl_tempbuf.h
>> index b6ad9ee6a46..e614a77bc4f 100644
>> --- a/libstdc++-v3/include/bits/stl_tempbuf.h
>> +++ b/libstdc++-v3/include/bits/stl_tempbuf.h
>> @@ -110,7 +110,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>> Â template<typename _Tp>
>> Â Â Â inline void
>> Â Â Â return_temporary_buffer(_Tp* __p)
>> -Â Â Â { ::operator delete(__p); }
>> +Â Â Â { ::operator delete(__p, std::nothrow); }
>
> This change is harmless, but unnecessary.
>
> The standard requires that the nothrow versions of operator new must
> obtain memory from the same source as the normal version of operator
> new (even if the user has replaced one or both versions of operator
> new). That means you can always use the normal version of operator
> delete instead of the nothrow one.
>
> If your tests failed because of this, then your replacement versions
> of operator new and operator delete were wrong.
>
> See [new.delete.single] p7.
>
Good to know that it is not such a big deal.
I might still propose to change it in stage 1. It is quite convenient to
play with std::nothrow operators overload to control temporary buffer
behavior without impacting other allocations that can take place during
the test execution.
More information about the Libstdc++
mailing list