4 Replies - 307 Views - Last Post: 29 January 2013 - 07:15 AM Rate Topic: -----

#1 tal1133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-May 12

C++ Debug Asserion Failed using delete function

Posted 29 January 2013 - 02:59 AM

Hi all,

I created a Queue<T> class in c++,
when I call the destructor and trying to delete the array I get the error:

_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

this is a short of my code:

template <class T> class Queue
{
    
private:
    int head, tail, size, max;
    T *arr;
    static int count;
    
public:
    Queue(int max=10);
    Queue(const Queue &q);
    ~Queue();
    static void init();
    static void print_count();
    bool isEmpty();
    bool isFull();
    bool enQueue(T value);
    bool deQueue(T *pValue);
	Queue merge(Queue<T> q1, Queue<T> q2);
};

template<class T> Queue<T>::Queue(int max = 10)
{
    this->max = max;
    size = 0;
    head = 0;
    tail = 0;
    count++;
    arr = new T[max];
}

template<class T> Queue<T>::Queue(const Queue &q)
{
    max = q.max;
    size = q.size;
    head = q.head;
    tail = q.tail;
    count++;
    arr = new T[max];
    for(int i =0; i < size; i++)
    {
        arr[(head + i) & max] = q.arr[(head + i) % max];
    }
}

template<class T> Queue<T>::~Queue()
{
    count--;
    delete[] arr;
}



calling it from the main:

Queue<int> q1;
q1.~Queue();



I know that there are lots of questions about this error, but I simply couldn't make mine to work.

I'd really appreciate your comments!

Thanks,
Tal

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Debug Asserion Failed using delete function

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,686
  • Joined: 25-December 09

Re: C++ Debug Asserion Failed using delete function

Posted 29 January 2013 - 05:48 AM

Why are you manually calling the destructor? Normally you don't manually call class destructors.

Also I don't recommend using default parameters in constructors, in my opinion it is better to overload the constructors instead.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 tal1133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-May 12

Re: C++ Debug Asserion Failed using delete function

Posted 29 January 2013 - 06:55 AM

Is the destructor being called automatically?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,686
  • Joined: 25-December 09

Re: C++ Debug Asserion Failed using delete function

Posted 29 January 2013 - 07:11 AM

Yes the destructor will be called automatically, when the class instance goes out of scope. You can test this by placing the class instance inside a function or a separate block:

#include <iostream>
using namespace std;

template <class T> class Queue
{
    
private:
    int head, tail, size, max;
    T *arr;
    static int count;
    
public:
    Queue(int max=10);
    Queue(const Queue &q);
    ~Queue();
    static void init();
    static void print_count();
    bool isEmpty();
    bool isFull();
    bool enQueue(T value);
    bool deQueue(T *pValue);
	Queue merge(Queue<T> q1, Queue<T> q2);
};

template<class T> Queue<T>::Queue(int max = 10)
{
    cout << "In Constructor " << endl;
    this->max = max;
    size = 0;
    head = 0;
    tail = 0;
    count++;
    arr = new T[max];
}

template<class T> Queue<T>::Queue(const Queue &q)
{
    max = q.max;
    size = q.size;
    head = q.head;
    tail = q.tail;
    count++;
    arr = new T[max];
    for(int i =0; i < size; i++)
    {
        arr[(head + i) & max] = q.arr[(head + i) % max];
    }
}

template<class T> Queue<T>::~Queue()
{
   cout << "In destructor" << endl;
    count--;
    delete[] arr;
}




int main()
{
   cout << "Entering Block" << endl;
   { 
      Queue<int> q1; // This will be destroyed at the end of the block.
   }
   cout << "Exiting Block" << endl;

   return(0);
}


If you place a print statements inside your constructor and destructor you can see that they are being called.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 tal1133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-May 12

Re: C++ Debug Asserion Failed using delete function

Posted 29 January 2013 - 07:15 AM

Thanks a lot,
never thought about it actually, even though it makes a lot of sense now..

Thank you again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1