Date: Sat, 23 Aug 2025 09:42:21 +0100
On Sat, 23 Aug 2025, 06:59 organicoman via Std-Proposals, <std-proposals_at_[hidden]> wrote:Actually it does work without breaking any code.You have to overload the delete operator in global scope.template <typename T>void delete(void*, T* &p){ delete p; p= nullptr;}And you do the same with the delete[ ].This is not necessary.When you write `delete p` the compiler already does more than just call `operator delete(p)`, because it also destroys `*p`The compiler can easily turn `delete p` into: std::destroy_at(p);operator delete(p);p = nullptr;Is there a flag to turn this on?This is already allowed today, you don't need to change the standard. It's not*required* by the standard because it adds a store that is usually redundant, but compilers could offer an option to do this.As others have pointed out, doing that is unlikely to prevent very many bugs in practice.Use after free, double free...etc will be more like to be caught in code{ m_ptr = new T; // if all the following take m_ptr by reference inspect_maybe_free(m_ptr); transform_maybe_free(m_ptr); maybe_free(m_ptr); // i can detect if delete was called before. if(m_ptr) delete m_ptr;}With the current implementation, you cannot do this.That doesn't matter. Zeroing the pointer can be done by the delete operator, not by the `operator delete` function. As far as I know, no compiler does this!Is there?
Received on 2025-08-23 08:42:38