Cannot access data struct members after looping a couple times.

Hi guys,
I have a a loop that creates OpenGL display lists and uses a data structure with data structures in it to create them, but every time I near the end of the loop, I get a segmentation fault. I looked through my locals/watchers and see that one of the data structures is unreadable even though it hasn't been changed. I'm quite confused as to why this is happening.

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
        void CSprite::GenerateGLDisplayList(SGfxSprite *aSprite, int aNumFrames)
        {
            uint displayList = glGenLists(aNumFrames);

            for(int i = 0; i < aNumFrames; ++i)
            {
                glNewList((displayList+i), GL_COMPILE);
                    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
                    glBindTexture(GL_TEXTURE_2D, aSprite->iGLTextures[i]);

                    glTexCoord2i(0, 0);
                    glVertex2f(aSprite->gfxObject->sPoly->sPosition->nX, aSprite->gfxObject->sPoly->sPosition->nY); // At the end of the loop, this is the line that seg faults. My locals/watchers are showing me that sPoly is NULL.

                    glTexCoord2i(1, 0);
                    glVertex2f(aSprite->gfxObject->sPoly->sDimensions->nX, aSprite->gfxObject->sPoly->sPosition->nY);

                    glTexCoord2i(1, 1);
                    glVertex2f(aSprite->gfxObject->sPoly->sDimensions->nX, aSprite->gfxObject->sPoly->sDimensions->nY);

                    glTexCoord2i(0, 1);
                    glVertex2f(aSprite->gfxObject->sPoly->sPosition->nX, aSprite->gfxObject->sPoly->sDimensions->nY);
                glEndList();

                aSprite->iFrames[i] = (displayList+i);
            }

            aSprite->currentFrame = 0;
        }


SGfxSprite declaration:
1
2
3
4
5
6
7
8
9
10
11
            struct SGfxSprite
            {
                uint *iGLTextures; // These are the OpenGL textures.
                uint iFrames[]; // These the OpenGL display lists.
                int currentFrame; // This is used when drawing to determine what frame of the sprite to draw.
                std::string str_Name; // The object name;
                SGraphicsObject *gfxObject; // The SGraphicsObject used for drawing.

                SGfxSprite(std::string aName, SGraphicsObject *aGfxObj) :
                    str_Name(aName), gfxObject(aGfxObj) {}
            };


SGraphicsObject declaration:
1
2
3
4
5
6
    struct SGraphicsObject
    {
        EPhysicsObjectShape eShape; // This enum tells what kind of object is being drawn.
        SPoly *sPoly; // This the polygon of which is used for drawing.
        SGraphicsObject(EPhysicsObjectShape aeShape, SPoly *asPoly) : eShape(aeShape), sPoly(asPoly) {}
    };


SPoly declaration:
1
2
3
4
5
6
7
8
9
    struct SPoly
    {
        SVec2D *sPosition; // The object's position in pixels.
        SVec2D *sDimensions; // IF OBJECT IS CIRCLE -> X = radius, Y = -1 ELSE dimensions are for quadrillaterals. (in pixels)
        bool isCircle; // Is this object a circle?

        SPoly(SVec2D *aPos, SVec2D *aDimen, bool abIsCircle = false) :
            sPosition(aPos), sDimensions(aDimen), isCircle(abIsCircle) {}
    };


SVec2D declaration:
1
2
3
4
5
6
7
    struct SVec2D
    {
        float nX;
        float nY;

        SVec2D(int aX, int aY) : nX(aX), nY(aY) {}
    };



I hope you guys can make sense as to why this is happening because I can't. I don't see how a member of a data structure can suddenly (even after no manipulation has happened) become NULL.
Last edited on
I have figured it out. All it took was time :)
It turns out that when I was writing to aSprite->iFrames[i] I was corrupting memory. (uh duhhh)
This was due to the member iFrames not being initialized on the heap. So it overwrote other members which was what causes such an error.
Topic archived. No new replies allowed.