ThisHandleNotInUse
ThisHandleNotInUse

Reputation: 1153

Trouble With Pointers - Getting Access Violations

I'm having trouble understanding the ways memory is allocated with pointers. I'm creating several "nested" structures because I need several different layers of complexity in accessing these structures... Here's the code:

struct HTCoord
{
    float Position[3];

    void DebugDisplay()
    {
        std::cout << "HTCoord:" << Position[0] << ", " << Position[1] << ", " << Position[2] << " ";
    }
};

struct HTColor
{
    float Color[4];

    void DebugDisplay()
    {
        std::cout << "HTColor: " << Color[0] << ", " << Color[1] << ", " << Color[2] << ", " << Color[3] << " ";
    }
};

struct HTVertex
{
    HTCoord Position;
    HTColor Color;

    void DebugDisplay()
    {
        Position.DebugDisplay();
        Color.DebugDisplay();           
    }
};

struct HTLblVertex
{
    UINT ID;
    HTVertex *pVertex;

    void DebugDisplay()
    {
        std::cout << "ID: " << ID << " ";
        pVertex->DebugDisplay();
        std::cout << "\r\n";
    }
};

class TestCube
{
    UVFace *Faces[6];
    HTLblVertex Vertices[8];
    HTVertex VertRefs[8];


public:
    TestCube()
    {
        HTVertex TestVertices[8] = {
            { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }, //0
            { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f }, //1
            { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f }, //2
            { 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f }, //3
            { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f }, //4
            { 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f }, //5
            { 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f }, //6
            { 1.0f, 1.0f, 1.0f, 0.2f, 0.2f, 0.2f, 1.0f }  //7
        };

        for (int i = 0; i < 8; i++)
        {
            VertRefs[i] = TestVertices[i];
            Vertices[i] = { i, &VertRefs[i] };  //the pointer resets to null after the constructor exits            
        }


    }
    ~TestCube()
    {
        for (int i = 0; i < 6; i++)
        {
            delete Faces[i];
        }
        delete Faces;
    }

    void DebugDisplay()
    {
        for (int i = 0; i < 6; i++)
        {
            std::cout << "FACE " << (i + 1)<< " ";
            Faces[i]->DebugDisplay();
        }
    }
};

In the final class object is where the trouble seems to arise. The variable "VertRefs" seems to stay constant and at the proper value, but the array of pointers "Vertices" no longer seems to reference it properly after leaving the constructor and calling "DebugDisplay" - they are all set to "null" or trying to read never never land. Why are the pointers uninitializing themselves? (obviously I'm declaring them improperly and don't understand what precisely is happening). Any help would be appreciated - I've recently been tinkering with C++ instead of C# and it's making me irritable :-)

EDIT: I've deleted the "UVFace" code because that's not the problem I asked, but there's still this problem with the pointers and everyone got excited about UVFace code.

Upvotes: 0

Views: 105

Answers (2)

ThisHandleNotInUse
ThisHandleNotInUse

Reputation: 1153

The issue appears to have been resolved. It appear the pointers are not actually deinitizializing themselves but it's a trick of the IDE being unable to identify their value once the code block has left the object and returned. The original memory access problem was related to creating some native arrays and passing them to "UVFace." When the native arrays went out of scope, the "UVFace" objects were left with dangling pointers.

Since nobody had answered it, I thought I would leave this answer as once in a while I'll find stray questions in Stack Overflow that I wish had been answered from years back.

For people trying to switch languages to C++ - the essence of my problem was two fold:

The first problem is that a pointer does not hold an object if that object is otherwise scoped within a function unless that object is declared with "new." The object passes out of scope and the pointer remains pointing to never never land.

The second problem is that Visual Studio apparently cannot necessarily discern what a member variable's pointer is pointing to if you leave a member function and come back to another member function which may falsely lead you to believe that the pointer isn't pointing to anything by examining it in the debugger.

I.E. Visual Studio was (falsely) telling me my "Vertices" array of pointers were at NULL after leaving the constructor and coming back. This led me to believe my break in the chain was earlier than later dangling pointers. See this question for further information on misleading debugger information if you're used to relying on the debugger to give you accurate information if you're used to C# or VB: Visual Studio not able to show the value of 'this' in release mode (with debug information)

Upvotes: 0

barak manos
barak manos

Reputation: 30166

delete Faces is most definitely a cause for a memory access violation.

This array is allocated statically, and should not be deallocated dynamically.

Upvotes: 4

Related Questions