I have a custom delete[] operator:
void operator delete[](void *ptr, char const* file, int line) noexcept {...}
When I try to call it, it calls the simple delete[](void *ptr) an not my custom operator:
char *b = new char[256];
delete[] b, __FILE__, __LINE__;
It compiles, but is my call to the custom operator right?
When using the delete expression the default operator delete(void*) is called. Similar for the array uses. The only case when an overloaded operator delete() is called by the language is when an exception is thrown by the constructor of the object after the match operator new() was called.
If you use a custom operator new(), i.e., using some placement syntax like new(a, b) T(...), you'll need to manually destroy the object and release the corresponding memory:
T* ptr = new(__FILE__, __LINE__) T(args);
// ...
ptr->~T(); // destroy the object
operator delete(ptr, __FILE__, __LINE__);
When replacing or overloading the "normal" operator new()/operator delete(), i.e., the signatures
void* operator new(std::size_t)
void operator delete(void*)
(and the corresponding array forms), either by replacing the global version or using a class-specific overload, i.e., a corresponding static member, the destructor and operator delete() are called by the delete expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With