I am trying to learn about memory pools in C ++ for better speed and debugging capabilities. I followed the approach found here: http://oroboro.com/overloading-operator-new/ . So I overloaded new , new[] , delete and delete[] as follows:
inline void* operator new ( size_t size ) { return myAlloc( size ); } inline void* operator new[] ( size_t size ) { return myAlloc( size ); } inline void operator delete ( void* ptr ) { myFree( ptr ); } inline void operator delete[]( void* ptr ) { myFree( ptr ); }
I like the fact that third-party libraries are aimed at this version of new , but I had a problem. I am making a DirectX application that uses DXUT. I compile DXUT separately from my project. In the end, it calls:
std::unique_ptr<D3D11_SUBRESOURCE_DATA[]> initData( new (std::nothrow) D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] );
As soon as this unique pointer goes out of scope, it crashes when calling delete[] _Ptr , which did not go through my overloaded operator. I tried to debug the implementation of the memory pool by adding int* dummy = new int[10]; delete[] dummy; int* dummy = new int[10]; delete[] dummy; in my main . Building the project gave a mistake, but clean construction worked great. To my surprise, everything worked, including the DXUT line, which was crashing!
Question 1: What exactly happened when I added a debug line that fixed the problem? I think for some reason my delete [] operator was not known until I named it in my own application code? Is it guaranteed to fix the problem or is it just stupid?
Question 2: I noticed that new (std::nothrow) D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] did not call my operator new[] directly, but called my operator new (without brackets). It still calls the delete[] operator on the pointer. This is problem? Should I add the appropriate overload to call my operator new[] or is this behavior fine?
For reference, the operator new[] overload that was called was:
void * __CRTDECL operator new[](::size_t count, const std::nothrow_t& x) _THROW0() {