系统&库规范:
操作系统- Ubuntu 11.10
显卡- ATI移动Radeon HD 5430
显卡驱动版本- fglrx-updates/fglrx-updates-dev (2:8.881-0ubuntu6.1)
SDL Version - libsdl1.2debian-all (SDL 1.2.14-6.1)
OpenGL Version - libgl1-mesa (7.7.1-5)
----4.1.11005兼容性配置文件背景信息
编译命令:gcc -Wall -Wextra -g -O3 -o $@ $^ -lSDL -lGLU -lGLEW -std=gnu99
包含库"stdlib.h" "stdio.h" "stdlib.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 ./mainhttp://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: %sn",SDL_GetError());
video = SDL_GetVideoInfo( );
if (video == NULL) fatal("Video query failed: %sn",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: %sn", SDL_GetError());
if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0) fatal("Video Attribute error: %sn", SDL_GetError());
if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0) fatal("Video Attribute error: %sn", SDL_GetError());
if(SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8) < 0) fatal("Video Attribute error: %sn", SDL_GetError());
if(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24) < 0) fatal("Video Attribute error: %sn", SDL_GetError());
if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) fatal("Double Buffering Init failed: %sn", SDL_GetError());
/* Create the surface */
surface = SDL_SetVideoMode(width,height,32,flags);
if (surface==NULL) fatal("Video Mode Set failed: %sn",SDL_GetError());
SDL_WM_SetCaption("Morbular","Morbular");
return surface;
}
void initGL()
{
GLenum err = glewInit();
if(!err == GLEW_OK) fatal("Glew Init failed: %sn",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 %sn",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 %sn",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 %sn",filename);
count = lseek(f, 0, SEEK_END);
if(count<0) fatal("Error reading data from file %sn",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 %sn",size,filename);
count = fread(text,sizeof(char),count,file);
text[count] = ' ';
}
fclose(file);
} else fatal("Error reading data from file %sn",filename);
}
return text;
}
printInfoLog(shader->ID);
glUseProgramObjectARB(shader->ID);
//Set uniforms
free(vertexString);
free(fragmentString);
//---- Code segement
如果你还需要什么就问我。请告诉我为什么会这样
我经常看到这样的东西,当我