Thread: Debug Assertion Failure - bad pointer

  1. #1
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Debug Assertion Failure - bad pointer

    Hi i have a simple problem,

    i keep receiving this error (see pic)

    I know its something to do with passing/using a bad pointer causing the assertion to fail. Im just unsure of where this occurs, heres my code:

    Code:
    template <int size> class Compartment : public Node {
    
    	public:
    
    		Compartment();
    		~Compartment();
    
    		Compartment* getCompartment() { return this; }
    
    		void printCompartments(ofstream o);
    		Node *find(int i);
    
    	private:
    
    		/*************/
    		/* Variables */
    		/*************/
    
    		Node *current;
    		Node *head;
    		Node *tail;
    
    		/*************/
    		/*  Methods  */
    		/*************/
    
    		Node *getNext(void)    { return current->next; }
    		Node *getPrev(void)    { return current->prev; }
    		Node *getHead(void)    { return head;          }
    		Node *getCurrent(void) { return current;       }
    		Node *getTail(void)    { return tail;          }
    
    		int length(void)       { return size;          }
    
    		double getReceptor(Node* n) { return n->receptor; }
    		double getLigand(Node* n)   { return n->ligand;   }
    
    		void setReceptor(Node *n, double conc);
    		void setLigand(Node *n, double conc);
    };
    Heres the constructor.. and possibly occurs somewhere in here...

    Code:
    template <int size> Compartment<size>::Compartment()
    {
    	int count = 0;
    	 
    	head = new Node;
    	current = head;
    	tail = current;
    
    	current->num = count;
    
    	for(int i = 0; i < size; i++)
    	{
    		current->next = new Node;
    		current->next->prev = current;
    	
    		count++;
    		
    		current->num = count;	
    		current->ligand = 0.0;
    		current->receptor = 0.0;
    
    		current = current->next;
    	}
    }
    
    template <int size> Compartment<size>::~Compartment() 
    { 
    	delete head; delete current; delete tail;
    	cout << "Compartment Destroyed" << endl; 
    }
    Anyone got any idea where to begin? Ive debugged this constructor and it "seems" to be allocating correctly.. i have a faint feeling its with the destructor? VC++ /Win XP btw
    Last edited by ventolin; 05-24-2004 at 08:59 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >current = head;
    >tail = current;
    At this point, head and tail point to the same location. Unless this changes at some point before destruction then you'll be deleting the memory twice. The same goes with current. If current is pointing to either head or tail when the destructor is called, you'll free the memory twice. On top of that, the destructor leaks memory because it only tries to delete the first, last, and current node in the list.
    My best code is written with the delete key.

  3. #3
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Ok i actually just noticed the first point, but thanks on the memory leak being pointed out

    Thanks

  4. #4
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Erm i dont really understand what im doing, but where is double memory delete happening? in the constructor? ive modified the list so tail actually points to the end, and head points to the front of the list, but im still getting the error.

    In the destructor do i need to just delete current?? even if head and tail points to this object?

    Argh i getting confused.. what do i delete? Do i assign head and tail to NULL or delete them? or delete head, tail and then current?

    Is this almost right?

    Code:
    template <int size> Compartment<size>::~Compartment() 
    { 
    	Node *n;
    	n = this.getTail();
    
    	for(int i = 0; i < this.length(); i++)
    	{
    		temp = tail->prev;
    		delete current;
    		current = temp;
    	}
    	head = current;
    	tail = current;
    
    	delete current;
    
    	cout << "Compartment Destroyed" << endl; 
    }
    Last edited by ventolin; 05-24-2004 at 09:58 AM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but where is double memory delete happening?
    In the destructor. When you destroy a linked list, you should only use delete on a single pointer.

    >Argh i getting confused..
    It's very simple. Start at the head and walk down to the tail, deleting each node as you go. Use a temporary pointer to save the next link:
    Code:
    template <int size> Compartment<size>::~Compartment() 
    {
      Node *save;
    
      for (current = head; current != tail; current = save) {
        save = current->next;
        delete current;
      }
      delete tail;
      cout << "Compartment Destroyed" << endl; 
    }
    Notice that I broke my own rule about only deleting memory using a single pointer. I did that because I noticed that you fail to terminate your list with a null pointer; you only terminate the list with tail, and tail also needs to be freed. If you set tail's link to be null then I could have done this:
    Code:
    template <int size> Compartment<size>::~Compartment() 
    {
      Node *save;
    
      for (current = head; current; current = save) {
        save = current->next;
        delete current;
      }
      cout << "Compartment Destroyed" << endl; 
    }
    My best code is written with the delete key.

  6. #6
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Ah I understand now, thanks for the explanation. I didnt realise you only delete one pointer, and thanks for pointing out i didnt make the tail into NULL hence i had you have to delete it.

    Anyway thanks Prelude,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Debug Assertion Failed!
    By IndioDoido in forum C Programming
    Replies: 31
    Last Post: 03-25-2008, 11:07 AM
  3. debug assertion failed!
    By chintugavali in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 04:05 AM
  4. debug assertion failure
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-20-2004, 11:23 AM
  5. Assertion failure while creating window
    By roktsyntst in forum Windows Programming
    Replies: 0
    Last Post: 02-10-2003, 08:18 PM