与OpenGL,SDL和图形驱动程序相关的内存泄漏和错误

时间:2011-11-04 22:25:55

标签: opengl memory-leaks segmentation-fault sdl ati

系统&图书馆规格:
操作系统 - Ubuntu 11.10
显卡 - ATI Mobility Radeon HD 5430
图形驱动程序版本 - fglrx-updates / fglrx-updates-dev(2:8.881-0ubuntu6.1)
SDL版本 - libsdl1.2debian-all(SDL 1.2.14-6.1)
OpenGL版本 - libgl1-mesa(7.7.1-5)
---- 4.1.11005兼容性配置文件上下文
编译命令:gcc -Wall -Wextra -g -O3 -o $ @ $ ^ -lSDL -lGLU -lGLEW -std = gnu99
包含的库“stdlib.h”“stdio.h”“stdarg.h”“string.h”“math.h”“SDL / SDL.h”“GL / glew.h”

问题描述 我正在尝试将OpenGL / GLUT应用程序移动到OpenGL / SDL应用程序中。 GLUT应用程序无错误地工作。

在GDB下,我收到以下分段错误:

Program received signal SIGSEGV, Segmentation fault.
__GI_getenv (name=0xb7f8af92 "L_MOUSE_RELATIVE") at getenv.c:90
90  getenv.c: No such file or directory.
    in getenv.c
(gdb) where
#0  __GI_getenv (name=0xb7f8af92 "L_MOUSE_RELATIVE") at getenv.c:90
#1  0xb7f69922 in ?? () from /usr/lib/libSDL-1.2.so.0
#2  0xb7f69922 in ?? () from /usr/lib/libSDL-1.2.so.0
#3  0xb7f30fa5 in SDL_PumpEvents () from /usr/lib/libSDL-1.2.so.0
#4  0xb7f30fe4 in SDL_PollEvent () from /usr/lib/libSDL-1.2.so.0
#5  0x080499df in mainloop (head=0x845f248) at mainloop.c:34

在整个程序中,我一直检查NULL和其他失败的返回,所以我决定运行Valgrind来查看代码中错误处理内存的位置。以下链接指向valgrind --log-file = memerrors.txt ./main的结果文件 http://www.2shared.com/document/1dnbZQPS/memerrors-simple.html

我通常不会关心SDL,OpenGL或图形驱动程序(fglrx)库中的内存错误,但我自己的代码中几乎没有内存泄漏或错误,所以我很好奇是否有人对此有任何想法。

int mainloop(void* head){
//Declare Standard Variables
MORB_Header* header = (MORB_Header*) head;
MORB_Renderer* render = header->render;
MORB_Light* light = render->light;

//Initialize Shader
GLShader* shader = glCreateShaders(header,"shader.vert","shader.frag");

//Load Textures for Use
texture[0] = glLoadTexture("rock.bmp");
texture[1] = glLoadTexture("rock_n.bmp");

//First Run Setup
header->scrUpd = 1;
glViewport(0,0, render->width,render->height);
//Check for Errors
GLenum errb = glGetError();
glProject(render->fov,render->aspect,render->zNear,render->zFar);   
errCheck("First Run Setup");

int morbexit = 0;
while(!morbexit)
{
    SDL_Event event;
    while(SDL_PollEvent(&event))
    {
                //Do Nothing
    }
    if(header->scrUpd){
            /* Working Display Function */
        }
    else SDL_Delay(5);
    morbexit = header->quit;
}
free(shader);
return 0;
}

如果我删除了事件循环,我没有收到分段错误,这让我觉得内存问题主要是在SDL中。

我的准备功能如下:

    int width=800, height=600;
    SDL_Surface* initSDL()
{
const SDL_VideoInfo* video;
SDL_Surface* surface;

//Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) fatal("Video Init failed: %s\n",SDL_GetError());

video = SDL_GetVideoInfo( );
if (video == NULL) fatal("Video query failed: %s\n",SDL_GetError());

int flags = SDL_OPENGL | SDL_DOUBLEBUF | SDL_HWPALETTE;
if (video->hw_available) flags |= SDL_HWSURFACE;
else flags |= SDL_SWSURFACE;
if (video->blit_hw) flags |= SDL_HWACCEL;

/* Sets up OpenGL Attributes */
if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) < 0) fatal("Video Attribute error: %s\n", SDL_GetError());
if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0) fatal("Video Attribute error: %s\n", SDL_GetError());
if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0) fatal("Video Attribute error: %s\n", SDL_GetError());
if(SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8) < 0) fatal("Video Attribute error: %s\n", SDL_GetError());
if(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24) < 0) fatal("Video Attribute error: %s\n", SDL_GetError());
if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) fatal("Double Buffering Init failed: %s\n", SDL_GetError());

/* Create the surface */
surface = SDL_SetVideoMode(width,height,32,flags);
if (surface==NULL) fatal("Video Mode Set failed: %s\n",SDL_GetError());
SDL_WM_SetCaption("Morbular","Morbular");

return surface;
}
void initGL()
{
GLenum err = glewInit();
if(!err == GLEW_OK) fatal("Glew Init failed: %s\n",glewGetErrorString(err));
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
errCheck("initGL()");
}
int main(int argc, char* argv[])
{
//Process command line arguments
for(int i=1; i<argc; i++) {
    if(!strcmp(argv[i],"-window")) {
        width = atoi(argv[++i]);
        height = atoi(argv[++i]);
        if(!(width && height)) {
            fatal("'-window' should be in the form '-window WIDTH HEIGHT'");
        }
    } else {
        printf("Argument %s is invalid...  ignored...", argv[i]);
    }
}

SDL_Surface* surface;
Header* header;

surface = initSDL();
initGL();

//Start Morbular
header = headerInit(surface);
mainloop(header);

//Cleanup on exit
SDL_Quit();

//Return
return EXIT_SUCCESS;
}

以及处理内存/ OpenGL / SDL的其他代码部分

    //---- Code segement
int size = sizeof(Header)+sizeof(Renderer)+sizeof(Light);
void* addr = malloc(size);
if (addr == NULL) fatal("Cannot allocate %d bytes of memory for Header file %s\n",size,stderr);
Header* header = (Header*) addr;
    //---- Code segement (following sets all attributes of header,render,etc)

    //---- Code segement
int size = sizeof(GLShader);
GLShader* shader = malloc(size);
if (shader == NULL) fatal("Cannot allocate %d bytes of memory for Shader Program %s\n",size,stderr);
    //---- Code segement

    //---- Code segement
glValidateProgramARB(shader->ID)

    //---- Code segement
    char* textFileRead(char *filename)   
    {
FILE *file;
char *text = NULL;

int f,count;
f = open(filename, O_RDONLY);
if (f < 0) fatal("Cannot open file %s\n",filename);

count = lseek(f, 0, SEEK_END);
if(count<0) fatal("Error reading data from file %s\n",filename);

close(f);

if (filename != NULL) {
    file = fopen(filename,"rt");
    if (file != NULL) {
        if (count > 0) {
            int size = sizeof(char)*(count+1);
            text = (char *)malloc(size);
            if(text==NULL) fatal("Cannot allocate %d bytes of memory for file read %s\n",size,filename);
            count = fread(text,sizeof(char),count,file);
            text[count] = '\0';
        }
        fclose(file);
    } else fatal("Error reading data from file %s\n",filename); 
}
return text;
}

printInfoLog(shader->ID);

glUseProgramObjectARB(shader->ID);
    //Set uniforms         

free(vertexString);
free(fragmentString);
    //---- Code segement

如果您还需要其他任何东西,请询问。请帮助我理解为什么会这样。

1 个答案:

答案 0 :(得分:1)

我经常看到这样的事情

  • 不要调用库初始化函数。(在我的情况下经常使用glew_init())
  • 在运行时安装了错误的位(x86 vs x64)库(错误的下载链接不适用于apt)但通常这会导致链接器错误
  • 在调试模式下编译时,运行时而不是dev库。 (忘了-dev在apt-get install lib-awesome-dev中)