Dereferencing Problem

Started by
20 comments, last by Alpha_ProgDes 17 years, 9 months ago
Hey everyone, once again another question. I have a Sprite Class, that holds a pointer to a GLuint called TextureHandle. My Tile Class creates an instance of Sprite, and has a function called CopyTextureTile(GLuint texture).

void Tile::CopyTextureTile(GLuint texture)
{
    TileSprite.TextureHandle = new GLuint;
    TileSprite.TextureHandle = &texture
}
There is what that function looks like. My texture manager has a function returning GLuint to get a private texture manager texture. That looks like this.

GLuint TextureManager::GetTexture(char TextureID)
{
    if(TextureID == '0')
        return TerrainTextures[0];
    else if(TextureID == '1')
        return TerrainTextures[1];
    else if(TextureID == '2')
        return TerrainTextures[2];
    else if(TextureID == '3')
        return TerrainTextures[3];
    else if(TextureID == '4')
        return TerrainTextures[4];
    else if(TextureID == '5')
        return TerrainTextures[5];
    else if(TextureID == '6')
        return TerrainTextures[6];
    else if(TextureID == '7')
        return TerrainTextures[7];
    else if(TextureID == '8')
        return TerrainTextures[8];
    else if(TextureID == '9')
        return TerrainTextures[9];
    else
        MessageBox(NULL, "That's not a valid texture", "RPG", MB_ICONEXCLAMATION);

    return NULL;
}

My Map class has a 100 x 100 array of "Tile." The constructor calls loadMap to load up the map from a file. It then takes the info from the file, and copies the textures into the Tile.Sprite.TextureHandle, via CopyTextureTile. That Constructor looks like this.

Map::Map()
{
    if(!LoadMap())
        MessageBox(NULL, "The map was not loaded", "RPG", MB_ICONEXCLAMATION);
    for(int yIndex = 0; yIndex < 100; yIndex++)
    {
        for(int xIndex = 0; xIndex < 100; xIndex++)
        {
            MapArray[yIndex][xIndex].ID = MapFileLines[yIndex][xIndex];
            MapArray[yIndex][xIndex].CopyTextureTile(TexMan.GetTexture(MapArray[yIndex][xIndex].ID));
        }
    }
}

MapFileLines[yIndex][xIndex] is a character that was previously read from a file. Later, when I want to render the tiles though, the TextureHandle gives me problems.In my Render Class, I have two functions, RenderTile, and RenderMap.

void Renderer::RenderTile(float x, float y, GLuint Texture)
{
    static int count=0;
    float outx, outy, outx2, outy2;
    outx = (100.0 / (x * 16.0)) - 1.5;
    outx2 = (100.0 / (x * 16.0)) - 1.4375;
    outy = (100.0 / (y * 16.0)) - 1.5;
    outy2 = (100.0 / (y * 16.0)) - 1.4375;
    glBegin(GL_QUADS);
        glBindTexture(GL_TEXTURE_2D, Texture);
        glTexCoord2f(0.0, 0.0); glVertex2f(outx, outy);
        glTexCoord2f(0.0, 1.0); glVertex2f(outx, outy2);
        glTexCoord2f(1.0, 1.0); glVertex2f(outx2, outy2);
        glTexCoord2f(1.0, 0.0); glVertex2f(outx2, outy);
    glEnd();
    count++;
}
void Renderer::RenderMap()
{
    for(int y = 0; y <= 100; ++y)
    {
        for(int x = 0; x <= 100; ++x)
        {
            RenderTile((float)x, (float)y, *TestMap.MapArray[x][y].TileSprite.TextureHandle); //This line
        }
    }
}

When I dereference the pointer to pass it as a GLuint, it has some sort of memory leak, and Windows comes up with a serious error. Could someone point out what I am doing wrong with my pointer? Sorry if I rambled and made things hard to understand. Thanks!
Advertisement
Three things:

1) If you assigning your texturehandle, then there is no reason to allocate it before hand. This is a memory leak.
void Tile::CopyTextureTile(GLuint texture){    TileSprite.TextureHandle = new GLuint;    TileSprite.TextureHandle = &texture}

2) What is the error message, exactly? It would probably be easier for us to help you if you post the entire error message.

3) Read Introduction to Debugging. It uses Visual Studio, but the concepts are applicable to other debuggers.
I took that line out, and it still comes up with serious error. I don't get an actual compiler error, It's just one of those windows popups. "RPG.exe has encountered a problem and needs to close. We are sorry for the inconvenience." And I can Send Error Report, or Don't Send. Hehe.
Quote:Original post by Niddles
I took that line out, and it still comes up with serious error. I don't get an actual compiler error, It's just one of those windows popups. "RPG.exe has encountered a problem and needs to close. We are sorry for the inconvenience." And I can Send Error Report, or Don't Send. Hehe.


Memory leak is not an error, because memory leaks usually go undetected unless your program runs for a long time and runs out of memory. You should run the program in the debugger, it will help you locate the real source of the problem.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
There should be a details button on the runtime-error message box.
Which IDE are you using?
I have tried debugger, but I can't seem to understand it, I try to watch variables, and they don't appear. Meh, when I run the debugger on Dev-Cpp, a Warning pops up that says "An Access Violation(Segmentation Fault) raised in your program." That's about all I can pull out of the debugger.
Are you certain that MapArray is 100x100?
You might be able to hit debug in that window too which might bring you to the exact line (or close too it) of where the issue is.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

That's what it's initialized as
Tile MapArray[100][100];

At Mike: I used the debugger, and came to the conclusion it was on the dereferencing line. Hm.
void Renderer::RenderMap(){  for(int y = 0; y < 100; ++y)  {    for(int x = 0; x < 100; ++x)    {      RenderTile((float)x, (float)y, *TestMap.MapArray[x][y].TileSprite.TextureHandle); //This line    }  }}

Note that indices go from 0-99 in an array of size 100.
I assume that you are setting TextureHandle to something meaningful as well.

This topic is closed to new replies.

Advertisement