title: Class-specific new and delete called inconsistently
body: <p>I'm working on implementing memory pools for my project.  The idea is to have any class which inherits from MemoryPoolObject automatically use the memory pools when allocated using new/delete.  The problem is that my overridden new and delete operators are only being called about half the time.  I suspected this early on, so I confirmed this by counting the number of times that the MemoryPoolObject constructor and destructor get called, and comparing that to the number of times that the overloaded new/delete are called.</p>
  
  <p>Here's what the class looks like:</p>
  
  <pre><code>class MemoryPoolObject
  {
  public:
      MemoryPoolObject()
      {
          // Count the number of times objects are constructed
          MemoryManager::get().objectConstructed();
      }
  
      virtual ~MemoryPoolObject()
      {
          // Count the number of times objects are destructed
          MemoryManager::get().objectDestructed();
      }
  
      static void* operator new (size_t s)
      {
          // Get memory from the pool, count the number of times called
          return MemoryManager::get().allocate(s);
      }
  
      static void operator delete (void* ptr, size_t s)
      {
          // Return memory to the pool, track a bunch of stats
          MemoryManager::get().deallocate(ptr, s);
      }
  };
  </code></pre>
  
  <p>Here are the usage stats for the project's unit tests when I make the base class for most of my classes inherit from MemoryPoolObject:</p>
  
  <pre><code>Memory Pool Usage:
     Object Size | Allocated   | Deallocated | Leaked      | Max Stored  | Storage Capacity
    -------------+-------------+-------------+-------------+-------------+------------------
              44 | 1           | 0           | 1           | 0           | 0
              52 | 15          | 8           | 7           | 1           | 1
              56 | 9           | 9           | 0           | 2           | 2
              68 | 6           | 0           | 6           | 0           | 0
              72 | 24          | 9           | 15          | 1           | 1
              92 | 25          | 17          | 8           | 1           | 1
             100 | 1           | 1           | 0           | 1           | 1
             152 | 88          | 1           | 87          | 1           | 1
             180 | 29          | 24          | 5           | 1           | 1
    -------------+-------------+-------------+-------------+-------------+------------------
         totals: | 198         | 69          | 129         | 8           | 8
    Total constructed: 422
    Total destructed: 133
  </code></pre>
  
  <p>As you can see, I'm getting about twice as many calls to the constructor/destructor as I am to the overloaded new/delete operators.</p>
  
  <p>It's also worth mentioning that very nearly all of the allocation is done from the same place:</p>
  
  <pre><code>template &lt;class T&gt;
  T* LoadableObject::instantiate(const CreationData&amp; cd)
  {
      T* obj = new(T);
  
      if (!obj-&gt;initialize(cd))
      {
          delete(obj);
          obj = NULL;
      }
  
      return obj;
  }
  </code></pre>
  
  <p>By setting a flag inside of allocate() and then checking it in objectConstructed() I was able to catch the place where the problem was happening - and I confirm that calls from here sometimes call the class's new operator and sometimes do not.</p>
  
  <p>To address some of the comments... </p>
  
  <p>There is no placement new in the library at this time.  There are a few cases where objects are created on the stack, but as I said in the previous paragraph, I've been able to confirm it happening from a plain old new.  Also, everything created on the stack is going to have its destructor called - there aren't nearly enough destructor calls.  Also, I've been able to set breakpoints and catch my delete operator not being used from a regular delete call.</p>
  
  <p>This is being used for a large modular AI library (here's an article on modular AI: <a href="https://www.msco.mil/DocumentLibrary/Journals/MSJournalFall2013.pdf" rel="nofollow noreferrer">https://www.msco.mil/DocumentLibrary/Journals/MSJournalFall2013.pdf</a>).  It's hard to show all the detail because there's a lot going on.  However, the basic idea is that you have a bunch of interfaces defining broad types of objects - nodes, considerations, utility values, etc.  Each of these interfaces may have many children, which are constructed using factories.  The factories are templated, and call into the instantiate function that I show above.  </p>
  
  <p>A typical class structures goes something like this.  The root class is MemoryPoolObject.  LoadableObject (which includes the instantiate function) inherits from that.  ComponentBase inherits from LoadableObject.  The interface class (NodeBase, ComponentBase, etc.) inherits from ComponentBase.  The concrete classes inherit from the interface classes.  There are some exceptions that inherit directly from LoadableObject... but this problem persists whether I make MemoryPoolObject a base class for LoadableObject or a multiply-inherited base class for ComponentBase.</p>
  
  <p>Interestingly, there is some indication (possibly false - I'm still debugging) that this might be a cross-project problem.  The unit tests exist in one project, and they load the AI project as a static library.  I'm wondering it the problem is that objects that belong to the unit test project don't call the class-specific new and delete in the AI project, even though they inherit from MemoryPoolObject.  Those objects are also in a different namespace - but I don't see why either of those things should be relevant (at least, in theory).</p>
  
  <p>I may try using a different compiler, and see if that changes the outcome.</p>