when doubling my stack it give me critical section error ! _ctrlvalidHeappointer
<p>I have written a class <code>Stack</code> whose both sides are used, i.e. two stacks in one.
One from <code>[0]</code> until <code>[someplace - 1]</code> and one from <code>[capacity-1]</code> until <code>[someplace +1]</code>.</p>

<p>Everything is fine but I have some problems with doubling the memory when my array is full.
My code works for doubling at first, but when it needs to double some more it gives me some weird error.</p>

<blockquote>
  <p>_ctrlvalidHeappointer</p>
  
  <p>critical section error</p>
</blockquote>

<p>This is my code. There is some explanation in the code too. When I push too many element in the stack it fails.</p>

<pre><code>    string firstname = "asasasasasaasasasasassasasasaasas";
    string secondname= "asasdasfregeasasasasasgergergererg";
        for (int i = 0; i &lt; firstname.length(); i++)
        {
            a.push_at_first(firstname.at(i));
        }
        for (int i = 0; i &lt; secondname.length(); i++)
        {
            a.push_from_end(secondname.at(i));
        }
</code></pre>

<p>It is my class</p>

<pre><code>using namespace std;
template &lt;class T&gt;
class Stack{
public:
    Stack();
    ~Stack();
    Stack(const Stack&lt;T&gt;&amp; ob);
    void double_size();
    void push_at_first(T mydata);
    void push_from_end(T mydata);
    T &amp; operator = (Stack&lt;T&gt; ob);
private:
    int top;
    int top2;
    T * stack;
    int capacity;
};
template &lt;class T&gt;
T&amp; Stack&lt;T&gt;::operator = (Stack&lt;T&gt; ob)
{
    if(capacity == ob.capacity){
        top = ob.top;
        top2 = ob.top2;
        for (int i = 0; i &lt; capacity; i++)
        {
            stack[i] = ob.stack[i];
        }
        return *this;}
    else
    {
        capacity = ob.capacity;
        stack = new T[capacity];
        for (int i = 0; i &lt; capacity; i++)
        {
            stack[i] = ob.stack[i];
        }

    }
}


template &lt;class T&gt;
Stack&lt;T&gt;::Stack (const Stack&lt;T&gt;&amp; ob) :capacity(ob.capacity)
{

    stack = new T[capacity];
    top = ob.top;
    top2=ob.top2;
    for (int i = 0; i &lt; capacity; i++)
    {
        stack[i] = ob.stack[i];
    }
}
template &lt;class T&gt;
 Stack&lt;T&gt;::~Stack()
  {
delete [] stack;
   }
template &lt;class T&gt;
Stack&lt;T&gt;::Stack()
{
    capacity = 17;
    top = 0;
    top2 = capacity-1;
    stack = new T[capacity];
}
template &lt;class T&gt;
void Stack&lt;T&gt;::push_at_first(T mydata)
{
    if ( (top + 1) == (top2 -1) ) //  1 : because I want to Be a Empty Space between Two Stack so i can tell the difference
        double_size();
    stack[++top] = mydata;
}

template &lt;class T&gt;
void Stack&lt;T&gt;::push_from_end(T mydata)
{
    if( (top + 1) == (top2 -1) ) //  1 : because I want to Be a Empty Space between Two Stack so i can tell the difference
        double_size();
    stack[--top2] = mydata;
}


template &lt;class T&gt;
void Stack&lt;T&gt;::double_size()
{
Stack&lt;T&gt; temp(*this);
capacity *= 2;
stack = new T[capacity];
top = temp.top;
top2 = capacity - (temp.capacity - temp.top2);// capacity - number of data in stack of temp ;

// if we have something in first stack then copy 0 to top elements of temp.stack to stack
if(top &gt; 0)
{
    for (int i = 0; i &lt;= top ; i++)
    {
        stack[i] = temp.stack[i];
    }
}
// There is Something Wrong Down here ! i can't figure out what !
if(top2 &lt; capacity - 1)
{
    for (int i = capacity-1; i &gt;= top2; i--)
    {
        stack[i] = temp.stack[i-(temp.capacity)];
    }
}
</code></pre>

<p>}</p>
