Search code examples
c++vectorstackstdl-systems

'iterator not dereferencable' C++ Stack


No idea what is going on here. I have a std stack of Vector3D's (a 3-dimensional vector class give to me by my tutor). I have pushed two Vector3D's on to the stack but when I go to go the top one with vectorStack.top() I am treated to this very unhelpful error:

Debug Assertion Failed!

Expression: deque iterator not dereferenceble

I thought maybe there was something wrong with the Vector3D copy constructor but I've tried to make my own quick Vector3D struct (deleting the old one I was given) and that doesn't work either!

Here is the code:

D3Dapp.cpp
if (!lsys.initialised)
{
    LSystem::ReproductionRule r1;
    r1.from = 'F';
    r1.to = "F+F-[FFF]";
    lsys.rules.push_back(r1);

    LSystem::ReproductionRule r2;
    r2.from = 'F';
    r2.to = "FF";
    lsys.rules.push_back(r2);

    lsys.result = lsys.generateResult("F", 0, 5);
    lsys.currentPosition = Vector3D(0.0f,0.0f,0.0f);
    lsys.vectorStack.push(lsys.currentPosition);

    lsys.initialised = true;
}

  lsys.result = "[FF]+FF";

if (!lsys.complete)
{
    for (int i=0; i < lsys.result.length();i++)
    {
  // F move forward
        if (lsys.result[i] == 'F')
        {
            float cosAng = cos(lsys.angle);
            float sinAng = sin(lsys.angle);

    Vector3D currentPosition(lsys.vectorStack.top());

            Vector3D newPosition = Vector3D(
                currentPosition.x + (cosAng * 0.5f), 
                currentPosition.y + (sinAng * 0.5f), 
                currentPosition.z);

            //lsys.vectorStack.push(newPosition);

            Vertex s, e;
            s.x = currentPosition.x;
            s.y = currentPosition.y;
            s.z = currentPosition.z;
            e.x = newPosition.x;
            e.y = newPosition.y;
            e.z = newPosition.z;

            startList.push_back(s);
            endList.push_back(e);

            lsys.currentPosition = newPosition;

    // + turn right
        } else if (lsys.result[i] == '+')
        {
            float rdn = 3.141592f / 180.0f;
            lsys.angle = (lsys.angle + 20.0f);

    // - turn left
        } else if (lsys.result[i] == '-')
        {
            float rdn = 3.141592f / 180.0f;
            lsys.angle = (lsys.angle - 20.0f);

    // [ push stack
        } else if (lsys.result[i] == '[')
  {
    lsys.vectorStack.push(lsys.currentPosition);

    // ] pop stack
  } else if (lsys.result[i] == ']')
  {
    lsys.vectorStack.pop();
  }
    }

    lsys.currentPosition = Vector3D(0.0f,0.0f,0.0f);
}

lsys.complete = true;

The stack in lsystem is just stack<Vector3D> vectorStack;

I've tried a variety of ways of assigning vectorStack.top() to current position but nothing seems to be working.

For completeness, here are the constructors to Vector3D:

Vector3D.h
Vector3D() {
    x = y = z = 0;
}

Vector3D(double a, double b, double c) {
    x = a;
    y = b;
    z = c;
}

Vector3D(const Vector3D& v) {
    x = v.x;
    y = v.y;
    z = v.z;
}

EDIT: To prove there is something in the stack screenshot

This is the line in stack which throws the error screenshot2


Solution

  • It turns out the framework I'm using (being forced to use) is built with very strict memory requirements in mind, there are several calls to ZeroMemory which seem to allocate this amount of memory and this amount of memory only.

    So when I start adding a load of dynamic memory structures like stacks and vectors everything goes to hell.

    ZeroMemory( this, sizeof(D3dApp) )
    

    After that removing the above I still had some issues but I followed Mark B's advice and now its up and running.

    Many thanks.