Q:

Why do I get the _Block_Type_Is_Valid (pHead->nBlockUse) error?

I don't know why I keep getting the _Block_Type_Is_Valid (pHead->nBlockUse) error. I understand it usually is because I am double deleting something but I only used delete once in the code. Here is the code as followed.
Box.h
#ifndef BOX_H_
#define BOX_H_
//containing the box methods
class Box {
public:
Box();
void setValue(int Value);
void setPrevious(Box* prev);
int getValue();
    Box* getPrevious();

private:
    int m_value;
    Box* m_previous;
};

#endif /* BOX_H_ */

StackOfBoxes.h
#ifndef BOX_H_
#define BOX_H_
//containing the box methods
class Box {
public:
Box();
void setValue(int Value);
void setPrevious(Box* prev);
int getValue();
Box* getPrevious();

private:
int m_value;
Box* m_previous;
};

#endif /* BOX_H_ */

Box.cpp
#include "Box.h"

//containers
Box::Box()
{
m_previous=nullptr;
int m_value = 0;
}
void Box::setPrevious(Box* prev)
{
m_previous = prev;
}
void Box::setValue(int val)
{
m_value = val;
}
int Box::getValue()
{
return m_value;
}
Box* Box::getPrevious()
{
return m_previous;
}

Main.cpp
#include <iostream> //std::cout std::cin
#include "StackOfBoxes.h" //StackOfBoxes

int main()
{
StackOfBoxes stack; //Create an empty stack allocated stack
StackOfBoxes* stackPtr = new StackOfBoxes(); //Create a heap allocated stack

int sizeOfStack;    //int we'll use later to store the size of the stack

//push some numbers onto the stack
for (int i = 1; i <= 10; i++)
{
    stack.push(i * 5);
    stackPtr->push(i * 5);
}

//Store the size of the stack before popping anything
sizeOfStack = stack.size();

std::cout << "There are " << sizeOfStack << " items on the stack" << std::endl;

//Think about why we don't use i<stack.size()
for (int i = 0; i < sizeOfStack; i++)
{
    std::cout << "Popping the top: " << stack.pop() << std::endl;
    //We won't pop anything from stackPtr
}

//Deleting a pointer calls the destructor for the object it points to
delete stackPtr;
}

Last but not least, the code where I think the error is spawning from
StackOfBoxes.cpp
#include "StackOfBoxes.h"
#include <iostream>

//sets top and size
StackOfBoxes::StackOfBoxes()
{
m_top = nullptr;
m_size = 1;

}

//if top has a number it will pop
StackOfBoxes::~StackOfBoxes()
{
while (m_top != nullptr)
{
    pop();
}
}
//but if its empty it will stop popping
bool StackOfBoxes::isEmpty() const
{
if(m_size==0)
{
    return true ;
}
else
{
    return false ;
}
}

//how big is the stack
int StackOfBoxes::size() const
{
return m_size;
}

//pushes the top to reveal the next one
void StackOfBoxes::push(int val)
{
Box* temp = new Box();
temp->setValue(val);
temp->setPrevious(m_top);
m_top = temp;
m_size++;
}

//takes the top off of the stack
int StackOfBoxes::pop()
{

Box* temp = m_top->getPrevious();
int topp = m_top->getValue();
delete m_top;
m_size--;
return topp;
}

Any guidance would be greatly appreciated. Thank you!

A:

int StackOfBoxes::pop()
{

Box* temp = m_top->getPrevious();
int topp = m_top->getValue();
delete m_top;
}

temp is assigned but never used. m_top is left a dangling pointer. You probably meant to do m_top = temp; in there somewhere.

