Free() problem, app crash

Hi everyone!

I get problem with code from tutorial NeHe OpenGL

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
int LoadGLTextures()
{
    AUX_RGBImageRec *TextureImage[1]; //Pointer

    /*
    typedef struct _AUX_RGBImageRec {
      GLint sizeX, sizeY;
      unsigned char *data;
    } AUX_RGBImageRec;
    */

    int Status=FALSE;

    memset(TextureImage,0,sizeof(void *)*1 );

    if (TextureImage[0]=LoadBMP("Data/NeHe.bmp"))
    {
        Status=TRUE;

        glGenTextures(1, &texture[0]);
        glBindTexture(GL_TEXTURE_2D, texture[0]);

        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);         
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);         
    }

    if (TextureImage[0])
    {
        if (TextureImage[0]->data)
        {
            free(TextureImage[0]->data); //This causes crash
        }
        free(TextureImage[0]); //Crash too
    }
    return Status;
}

AUX_RGBImageRec *LoadBMP(char *Filename)
{

    FILE *File=NULL;
    if (!Filename) 
    {
        return NULL; 
    }

    File=fopen(Filename,"r");

    if (File)
    {
        fclose(File);
        return auxDIBImageLoad(Filename);
    }

    return NULL;
}


I get crash on OS:Windows Vista Home Basic, IDE:C::B, Compiler GCC on MinGW

but I don't get why app crashed
Last edited on
You should never free() something you didn't malloc(). You did not allocate TextureImage[0]->data or TextureImage[0] with malloc, so you must not try to free them.

The only allocation here was done with auxDIBImageLoad. So you'll have to look up that specific function and see what it's proper method of cleanup is.


In my attempt to google this function, I could not find any documentation on it. And in fact, everything I found says that it is a temporary/slapped together nonsense lib that you should not use.

http://www.gamedev.net/topic/352802-about-auxdibimageload/


So yeah.... I would find a different tutorial.
Thanks for reply

I will choose another library with different functions, more proffesional :)

Topic archived. No new replies allowed.