What happens if the new one is overloaded but the corresponding deletion is not loaded?

Can someone explain what happens if the new one is overloaded, but the corresponding deletion is not loaded in C ++?

+4
source share
1 answer

This is only a problem when an object construct throws an exception, and it is described in C ++ 11 5.3.4 / 18:

If the unambiguous function of maladjustment of the correspondence cannot be found, the propagating exception does not free up the memory of objects. [Note: this is suitable when the called distribution function does not allocate memory; otherwise it will most likely lead to memory leak. -end note]

Example:

T * p = new (true, 'x', Blue) T("Jim"); 

If the T constructor throws, we need an operator delete(void *, bool, char, enum Color) overload operator delete(void *, bool, char, enum Color) , either in the namespace area or as a static member of T , and if this function does not exist, then the release function is not called.

As noted in the note, in the case of posting-new features that are essentially no-ops, this may not be a problem. However, if the distribution function does nontrivial work, then there will be no corresponding cleaning function.

+5
source

Source: https://habr.com/ru/post/1438720/


All Articles