1

does anyone know whats wrong with this ?? it keeps giving me an "Assertion Failed" _BLOCK_TYPE_IS_VAILD(pHead->nBlockUse) when it tries to use the detructor on a non empty stack EDIT:MORE CODES:

class stack
{
private:
   struct StackNode
   {
      int x;
      int y;
      StackNode *next;  
   };

   StackNode *top;     

public:

   stack()
      {  top = NULL; }

   ~stack();

 stack::~stack()
    {
        StackNode *nodePtr,*nextNode;
            nodePtr=top;
            while (nodePtr!=NULL) 
            { 
                nextNode=nodePtr->next;
                delete nodePtr;
                nodePtr=nextNode;
            }   
    }

main.cpp

mouse_position.push(mouse_x,mouse_y);
print_stack(mouse_position);

void print_stack(stack m)
{
    int tempx=0;
    int tempy=0;
//  while(!m.isEmpty()){
//      m.pop(tempx,tempy);
    cout<<tempx<<tempy<<endl;
//  }

}
kingcong3
  • 193
  • 1
  • 2
  • 14
  • 5
    You mangled your memory somewhere and this isn't enough to go by. Did you write a proper copy constructor and assignment operator for `stack`? – Lightness Races in Orbit Mar 15 '11 at 20:17
  • 1
    @king: You should definitely add more code to this question, for example, what the definition of a `StackNode` is, and all the constructors/assignment/copy operators of `stack`, and which ones you are using. Basically, a minimal code example that we can compile, that will repro your problem. – Merlyn Morgan-Graham Mar 15 '11 at 20:20
  • more codes added, i think these are the main ones – kingcong3 Mar 15 '11 at 20:25
  • @Tomalak Geret'kal i didnt write a copy constructor, is it needed?? how would i do so for a stack? – kingcong3 Mar 15 '11 at 20:26
  • 2
    @kingcong: Read about the [Big Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – Björn Pollex Mar 15 '11 at 20:29
  • You don't show how to add items to the stack. That is, how to get a non-empty stack Since your problem is in a non-empty stack, that might be important information. – Max Lybbert Mar 15 '11 at 20:39
  • @kingcong3: That's more than likely your problem, then. Whenever you make a copy of your `stack` object, the pointers are copied. When one of the copies dies, your stack contents are freed. When the second copy dies you're attempting to free the same stack contents again, leading to your error. Your copy constructor [ought to do a deep copy](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) of the entire structure and its contents, and your assignment operator should invoke it. – Lightness Races in Orbit Mar 15 '11 at 23:25

3 Answers3

4

From the code you posted, it seems the problem is the missing copy constructor. Consider the following code:

stack mouse_position;
// fill mouse_position
print_stack(mouse_position);

When you call print_stack, you make a bitwise copy of mouse_position. When print_stack exits, that copy (namely m) is destroyed, calling delete on its top member, which is exactly the same top of mouse_position. When mouse_position is deleted, you're deleting its top twice.

Then again, there could be more bugs lurking in the code you haven't posted yet.

Pablo
  • 8,644
  • 2
  • 39
  • 29
2

Some things to look at:

  1. StackNode has no destructor.
  2. There are two definitions of stack destructor.
  3. Missing closing brace for class stack.
  4. Consider moving StackNode outside of the class.
  5. Prefer using std::stack and std::list.

Edit your post to show more context for more detailed help.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
2

You copy your stack when passed into print_stack and didn't follow the rule of three What is The Rule of Three? which results in a double deletion and your problem.

Unless you're doing this as an exercise, use std::stack instead - it's been tested and debugged for years.

Community
  • 1
  • 1
Mark B
  • 95,107
  • 10
  • 109
  • 188