From b2e25470ed7e093dfb6d0db7f343e8d92229186b Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 25 Feb 2023 21:23:37 +0100 Subject: [PATCH] WIP: solved segfault but need to improve gl_area_array --- Makefile | 5 +++++ include/base.h | 17 +++++++++-------- manifest.scm | 1 + src/graphics/graphics.c | 38 ++++++++++++++++++++++++++------------ 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 137802f..5e9c7d6 100644 --- a/Makefile +++ b/Makefile @@ -126,6 +126,11 @@ debug: build_system @gdb $(BINDIR)/gem-graph-client @echo -e ${CL2}[$@] ${CL}done.${CL3} +valgrind: build_system + @echo -e ${CL2}[$@] ${CL}executing...${CL3} + @valgrind $(BINDIR)/gem-graph-client + @echo -e ${CL2}[$@] ${CL}done.${CL3} + all: build_system @echo -e ${CL2}[$@] ${CL}done.${CL3} diff --git a/include/base.h b/include/base.h index c390fba..66797d7 100644 --- a/include/base.h +++ b/include/base.h @@ -47,31 +47,32 @@ enum * * @return void */ -static inline bool read_file(char *filename, char **contents) +static inline char *read_file(char *filename) { int fd; int filesize; + char *contents; fd = open(filename, O_RDONLY); if(fd < 0) { printf("Couldn't read file: %s\n",filename); - return false; + return NULL; } - filesize = lseek(fd, 0, SEEK_END) +1 ; - *contents = calloc(filesize, sizeof(char)); + filesize = lseek(fd, 0, SEEK_END) + 1 ; + contents = malloc(filesize * sizeof(char)); if (contents == NULL) { perror("Not enough memory to allocate file"); - return false; + return NULL; } lseek(fd, 0, SEEK_SET); - read(fd,*contents,filesize); + read(fd,contents,filesize); - *(contents+filesize-1)='\0'; + contents[filesize-1] = '\0'; close(fd); - return true; + return contents; } diff --git a/manifest.scm b/manifest.scm index 39b777c..4a33c1a 100644 --- a/manifest.scm +++ b/manifest.scm @@ -9,6 +9,7 @@ "coreutils" "gcc-toolchain" "pkg-config" + "valgrind" "findutils" "gdb" "make" diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index ff0c9f1..846f56f 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -251,14 +251,14 @@ static inline GLuint create_shader(int type, const char *src) glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len); - buffer = g_malloc(log_len + 1); + buffer = malloc(log_len + 1); glGetShaderInfoLog(shader, log_len, NULL, buffer); g_warning("Compile failure in %s shader:\n%s", type == GL_VERTEX_SHADER ? "vertex" : "fragment", buffer); - g_free(buffer); + free(buffer); glDeleteShader(shader); @@ -328,7 +328,8 @@ static inline short gl_area_size(void) */ static bool graphics_init_shaders(void *gl_area) { - char *file_content; + char *vertex_shader; + char *fragment_shader; struct gl_area_entry *entry; int status; GLuint vertex, fragment; @@ -346,25 +347,28 @@ static bool graphics_init_shaders(void *gl_area) } // Load vertex shader file - if (read_file(VERTEX_SHADER_FILE, &file_content) == false) + vertex_shader = read_file(VERTEX_SHADER_FILE); + if (vertex_shader == NULL) return false; - vertex = create_shader(GL_VERTEX_SHADER, file_content); - free(file_content); + vertex = create_shader(GL_VERTEX_SHADER, vertex_shader); if(vertex == 0) { entry->program = 0; + free(vertex_shader); return false; } // Load fragment shader file - if (read_file(VERTEX_SHADER_FILE, &file_content) == false) + fragment_shader = read_file(FRAG_SHADER_FILE); + if (fragment_shader == NULL) return false; - fragment = create_shader(GL_FRAGMENT_SHADER, file_content); - free(file_content); + fragment = create_shader(GL_FRAGMENT_SHADER, fragment_shader); if(fragment == 0) { glDeleteShader(vertex); entry->program = 0; + free(vertex_shader); + free(fragment_shader); return false; } @@ -393,7 +397,13 @@ static bool graphics_init_shaders(void *gl_area) glDeleteProgram(program); program = 0; - goto out; + glDeleteShader(vertex); + glDeleteShader(fragment); + + free(vertex_shader); + free(fragment_shader); + + return false; } /* Get the location of the "mvp" uniform */ @@ -404,7 +414,6 @@ static bool graphics_init_shaders(void *gl_area) glDetachShader(program, vertex); glDetachShader(program, fragment); -out: glDeleteShader(vertex); glDeleteShader(fragment); @@ -413,6 +422,9 @@ out: entry->v = v; entry->p = p; + free(vertex_shader); + free(fragment_shader); + return true; } @@ -525,8 +537,10 @@ bool graphics_init(void *gl_area) gl_area_array[array_size]->ptr = gl_area; + if (!graphics_init_shaders(gl_area)) return false; + graphics_init_buffers(gl_area); - graphics_init_shaders(gl_area); + glDebugMessageCallback(graphics_debug_callback, NULL); return true;