Reputation: 41
System & Library Specications:
Operating System - Ubuntu 11.10
Graphics Card - ATI Mobility Radeon HD 5430
Graphics Driver Version - 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 Compatibility Profile Context
Compile Commands: gcc -Wall -Wextra -g -O3 -o $@ $^ -lSDL -lGLU -lGLEW -std=gnu99
Included libraries "stdlib.h" "stdio.h" "stdarg.h" "string.h" "math.h" "SDL/SDL.h" "GL/glew.h"
Problem Description I'm trying to move a OpenGL/GLUT application into an OpenGL/SDL application. The GLUT applications works error free.
Under GDB I recieve the following segmentation fault:
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
Throughout my entire program I consistently check for NULL and other failed returns, so I decided to run Valgrind to see where memory was being mishandled in my code. The following links to the resulting file of valgrind --log-file=memerrors.txt ./main http://www.2shared.com/document/1dnbZQPS/memerrors-simple.html
I typically wouldn't care about the memory errors in the libraries for SDL, OpenGL, or the Graphics Driver(fglrx), but there are almost undoubtedly no memory leaks or errors in my own code, so I am curious if anyone has any thoughts on it.
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;
}
If I remove the event loop, I do not receive the segmentation fault which makes me think the memory problem is primarily in SDL.
My preparation functions follows:
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;
}
and other parts of code that deal with memory/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
If you need anything else just ask. Please help me understand why this happens.
Upvotes: 2
Views: 1530
Reputation: 7466
I usually see things like this when I
Upvotes: 1