After Java, C++ impresses me with his manual memory management and the idea of how many surprises a developer may meet on his/her way. For example, if you define your own operator new with custom arguments (it is called "class-specific placement allocation functions"), a placement form of operator delete that matches the placement form of operator new has to be defined (the corresponding form of the delete operator must have the signature void operator delete(void *ptr, user-defined-args...)). When an exception is thrown in a class constructor the placement form of operator delete will be invoked and take care of the memory reclaimation. If there is no the placement form of the operator, no one will be able to take care of the memory and a memory leak occurs.
Fortunately, the MSVC compiler lets us know about the dangerous using warning C4291 no matching operator delete found; memory will not be freed if initialization throws an exception (please, compile the code with /EHsc /W1). Let's have a look:
[1/2] Building CXX object src\memory\CMakeFiles\placement-new-delete.dir\PlacementNewDelete.cpp.obj
..\src\memory\PlacementNewDelete.cpp(67): warning C4291: 'void *MyClassA::operator new(size_t,MyAllocator &)': no matching operator delete found; memory will not be freed if initialization throws an exception
..\src\memory\PlacementNewDelete.cpp(32): note: see declaration of 'MyClassA::operator new'
[2/2] Linking CXX executable src\memory\placement-new-delete.exe
The warning is described in detail there: Compiler Warning (level 1) C4291 (MSDN).
The problem you can be faced is the following: in some cases, it's not so easy to implement the placement form of operator delete. Operator new forewer takes the size argument - the ammount of memory required for the object while operator delete doesn't but the argument may be necessary to allocate the memory and return it to the operating system then.
Let's consider the following pattern: a third-party memory allocator is used to allocate memory for objects, the allocator has two methods: allocate and deallocate and each method takes a size_t size parameter. I see this code very often through the Eclipse OMR JIT compiler, so I believe the pattern is quite popular.
Warning C4291
Fortunately, the MSVC compiler lets us know about the dangerous using warning C4291 no matching operator delete found; memory will not be freed if initialization throws an exception (please, compile the code with /EHsc /W1). Let's have a look:
[1/2] Building CXX object src\memory\CMakeFiles\placement-new-delete.dir\PlacementNewDelete.cpp.obj
..\src\memory\PlacementNewDelete.cpp(67): warning C4291: 'void *MyClassA::operator new(size_t,MyAllocator &)': no matching operator delete found; memory will not be freed if initialization throws an exception
..\src\memory\PlacementNewDelete.cpp(32): note: see declaration of 'MyClassA::operator new'
[2/2] Linking CXX executable src\memory\placement-new-delete.exe
The warning is described in detail there: Compiler Warning (level 1) C4291 (MSDN).
The problem you can be faced is the following: in some cases, it's not so easy to implement the placement form of operator delete. Operator new forewer takes the size argument - the ammount of memory required for the object while operator delete doesn't but the argument may be necessary to allocate the memory and return it to the operating system then.
Writing your own allocator
Let's consider the following pattern: a third-party memory allocator is used to allocate memory for objects, the allocator has two methods: allocate and deallocate and each method takes a size_t size parameter. I see this code very often through the Eclipse OMR JIT compiler, so I believe the pattern is quite popular.