Generic Memory allocation crash for overloding new and delete in C++
<p>I am having following code. Following code is crashing when I am deleting the allocated memory i.e., Rational::deleteMemPool();</p>

<p>Getting output as</p>

<blockquote>
  <blockquote>
    <p>mempool value is 00000000  mempool value is 003462E8  mempool value is 003462E8</p>
  </blockquote>
</blockquote>

<p>// Here for delete crashing though we have valid pointer.</p>

<p>Can any one please help me what is bug here?</p>

<pre><code>#include &lt;string&gt;
#include &lt;iostream&gt;


template &lt; class T &gt;
class MemoryPool {
public:
    MemoryPool (size_t size = EXPANSION_SIZE);
    ~MemoryPool ();
    inline void* alloc (size_t size); // Allocate a T element from the free list.
    inline void free (void *someElement); // Return a T element to the free list.
private:
    MemoryPool&lt;T&gt; *next; // next element on the free list.
    enum { EXPANSION_SIZE = 32}; // If the freeList is empty, expand it by this amount.
    void expandTheFreeList(int howMany = EXPANSION_SIZE); // Add free elements to the free list
};

template &lt; class T &gt; MemoryPool &lt; T &gt; :: MemoryPool (size_t size) {
    expandTheFreeList(size);
}

template &lt; class T &gt; MemoryPool &lt; T &gt; :: ~MemoryPool () {
    MemoryPool&lt;T&gt; *nextPtr = next;
    for (nextPtr = next; nextPtr != NULL; nextPtr = next) {
        next = next-&gt;next;
        delete [] nextPtr;
    }
}

template &lt; class T &gt; inline void* MemoryPool &lt; T &gt; :: alloc (size_t) {
    if (!next) {
        expandTheFreeList();
    }

    MemoryPool&lt;T&gt; *head = next;
    next = head-&gt;next;
    return head;
}

template &lt; class T &gt; inline void MemoryPool &lt; T &gt; :: free (void *doomed) {

    MemoryPool&lt;T&gt; *head = static_cast &lt;MemoryPool&lt;T&gt; *&gt; (doomed);
    head-&gt;next = next;
    next = head;
}

template &lt; class T &gt; void MemoryPool &lt; T &gt; :: expandTheFreeList(int howMany) {
    // We must allocate an object large enough to contain the next pointer.
    size_t size = (sizeof(T) &gt; sizeof(MemoryPool&lt;T&gt; *)) ? sizeof(T) : sizeof(MemoryPool&lt;T&gt; *);
    void *pNewAlloc = new char[size];
    MemoryPool&lt;T&gt; *runner = static_cast &lt;MemoryPool&lt;T&gt; *&gt; (pNewAlloc);
    next = runner;
    for (int i = 0; i &lt; howMany ; i++) {
        void *pNewAlloc = new char[size];
        runner-&gt;next = static_cast &lt;MemoryPool&lt;T&gt; *&gt; (pNewAlloc);
        runner = runner-&gt;next;
    }
    runner-&gt;next = 0;
}

class Rational {
public:
    Rational (int a = 0, int b = 1 ) : n(a), d(b) {}
    void *operator new(size_t size) { return memPool-&gt;alloc(size); }
    void operator delete(void *doomed,size_t size)  { memPool-&gt;free(doomed); }

    static void newMemPool() { 
        std::cout &lt;&lt; "mempool value is " &lt;&lt; Rational::memPool &lt;&lt; std::endl;
        memPool = new MemoryPool &lt;Rational&gt;; 
        std::cout &lt;&lt; "mempool value is " &lt;&lt; Rational::memPool &lt;&lt; std::endl;
    }
    static void deleteMemPool() { 
        std::cout &lt;&lt; "mempool value is " &lt;&lt; Rational::memPool &lt;&lt; std::endl;
        delete memPool; 
    }

private:
    int n; // Numerator
    int d; // Denominator
    static MemoryPool &lt;Rational&gt; *memPool;
};

MemoryPool &lt;Rational&gt; *Rational::memPool = 0;
int main() {

    Rational *array[1000];

    Rational::newMemPool();


    // Start timing here
    for (int j = 0; j &lt; 1; j++) {
        for (int i = 0; i &lt; 10; i++) {
            array[i] = new Rational(i);
        }

        for (int i = 0; i &lt; 10; i++) {
            delete array[i];
        }
    }

    // Stop timing here
    Rational::deleteMemPool();

}
</code></pre>

<p>Stacktrace:</p>

<pre><code>Stack trace:&gt;
ReadParsing.exe!Rational::deleteMemPool() Line 75   C++
ReadParsing.exe!main() Line 107 C++
ReadParsing.exe!__tmainCRTStartup() Line 586 + 0x19 bytes   C
ReadParsing.exe!mainCRTStartup() Line 403   C
kernel32.dll!7c817077()
</code></pre>
