From 4930a32c4409d8825ac4fbb54595e473f6242cd8 Mon Sep 17 00:00:00 2001 From: amy Date: Tue, 18 Apr 2023 19:28:29 -0500 Subject: better logging, less leaks i think --- readme.md | 4 ++-- src/glfww.c | 1 + src/point.c | 26 +++++++++++++++++++++----- src/util.c | 13 +++++++++++++ src/util.h | 4 ++++ 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/readme.md b/readme.md index 9b4ae3b..6f29db3 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,5 @@ -optimized build : `clang ./src/*.c -lm -lglfw -lGL -fno-trapping-math` +optimized build : `clang ./src/*.c -lm -lglfw -lGL -lGLEW -fno-trapping-math` #known issues -- glfw causes memory leak (not lost, related to video drivers) +- glfw & glew causes memory leak (not lost, related to video drivers) diff --git a/src/glfww.c b/src/glfww.c index 91bf7cd..3203bcd 100644 --- a/src/glfww.c +++ b/src/glfww.c @@ -25,6 +25,7 @@ GLFWwindow* glfw_init(){ int w,h; glfwGetFramebufferSize(window,&w,&h); glViewport(0,0,w,h); + info("yay:D i made a window uwu"); return window; /* while(!glfwWindowShouldClose(window)){ diff --git a/src/point.c b/src/point.c index 03c72cc..4513625 100644 --- a/src/point.c +++ b/src/point.c @@ -265,8 +265,11 @@ point_arr* square_gen(double* tl, double* tr, double* bl, double*br){ join_cords(a,c); join_cords(a,d); free(b->c); + free(b); free(c->c); + free(c); free(d->c); + free(d); return a; } point_arr* cube_gen(double* tl, double* tr, double* bl, double*br, @@ -277,22 +280,29 @@ point_arr* cube_gen(double* tl, double* tr, double* bl, double*br, double s; join_cords(a,b); free(b->c); + free(b); point_arr* c = square_gen(tl2,tr2,tl,tr); join_cords(a,c); free(c->c); + free(c); point_arr* d = square_gen(bl2,br2,bl,br); join_cords(a,d); - free(d->c); + free(d->c); + free(d); point_arr* e = square_gen(tl,tl2,bl,bl2); join_cords(a,e); - free(e->c); + free(e->c); + free(e); point_arr* f = square_gen(br2,br,tr2,tr); join_cords(a,f); - free(f->c); + free(f->c); + free(f); return a; } -int main(){ +int main(int argc,char*argv[]){ + flag_handle(argc,argv); + atexit(sig_handle); GLFWwindow* w = glfw_init(); refresh_size(w); GLenum err = glewInit(); @@ -304,6 +314,7 @@ int main(){ GLuint fid = fshader_comp(fshader_src); prog = build_shader(vid,fid); glUseProgram(prog); + info("built shaders"); double tl2[3] = {5.0,200.0,400.0}; double tr2[3] = {200.0,200.0,400.0}; double bl2[3] = {5.0,5.0,200.0}; @@ -401,8 +412,13 @@ int main(){ if(glfwWindowShouldClose(w))break; } - free(a->c); + free(a->c); + free(a); glfwDestroyWindow(w); win_clean(); + glDeleteShader(vid); + glDeleteShader(fid); + glDeleteShader(prog); + info("killed window:p"); return 0; } diff --git a/src/util.c b/src/util.c index e256dc1..f12b95b 100644 --- a/src/util.c +++ b/src/util.c @@ -2,6 +2,15 @@ #include #include "util.h" #include "strings.h" +double allocs = 0; +//#define malloc(X) mmalloc(X); +void mmalloc(){ + allocs++; +} +//#define free(X) ffree(X); +void ffree(){ + allocs--; +} int log_level = 0; int __signal = 0; void pexit(int s){ @@ -9,6 +18,10 @@ void pexit(int s){ exit(s); } void sig_handle(void){ + if(allocs>0) + warn("uneven allocations, memory leak(s)"); + if(allocs==0) + info("even allocations, no internal leaks"); if(__signal==0){ printf("\x1b[90mexited with \x1b[32m\x1b[1msignal [ %i ] \x1b[0m\x1b[90mgraceful exit\x1b[0m\n",__signal); } else if(__signal>0){ diff --git a/src/util.h b/src/util.h index 5a730f3..7e900a7 100644 --- a/src/util.h +++ b/src/util.h @@ -6,6 +6,10 @@ #include "string.h" #define greater(a,b) (a>b?a:b) #define lesser(a,b) (a>b?b:a) +#define malloc(X) malloc(X); mmalloc(); +void mmalloc(); +#define free(X) free(X); ffree(); +void ffree(); void err_m(char*,void (*)(int),char*,int); void warn_m(char*,char*,int ,...); void info_m(char*,char*,int ,...); -- cgit v1.2.3