Can't delete in c++
<p>I have a simple doubly linked list example that I'm working on, but for some reason I  keep getting the error: <code>pointer being freed was not allocated</code> when I try to delete</p>

<p>Here is a code snipet of the insert at end of list method</p>

<pre><code>template &lt;class T&gt;
void List&lt;T&gt;::insertAtEnd(T *o)
{
    node *newNode = new node;
    newNode-&gt;o = o;
    newNode-&gt;next = NULL;
    newNode-&gt;prev = last;
    if(last != NULL)
        last-&gt;next = newNode;
    last = newNode;
    if(first == NULL)
        first = newNode;
    delete(newNode);            // This delete call will usually be in another method
}                               // It is just here right now for testing
</code></pre>

<p>This class has two instance fields, a pointer to the beginning of the list and a pointer to the end, first and last respecitvely.  Each node is a struct as follows:</p>

<pre><code>struct node {
    node   *next;
    node   *prev;
    T      *o;
};
</code></pre>

<p>So far I have only tested with inserting 1 node, but I always get the same error.  However, if I comment out the lines</p>

<pre><code>if(first == NULL)
    first = newNode;
</code></pre>

<p>it works.  Help please? Thanks</p>

<p>EDIT: Both first and last start as <code>NULL</code>.</p>
