From c43614ce0893ec5bb49e9f710a3cf72bff6d7b87 Mon Sep 17 00:00:00 2001 From: a bellenir Date: Sat, 26 Jul 2014 01:01:11 +0000 Subject: [PATCH] fix memory leaks --- source/screen.c | 13 ++++++++++++- source/workdir.c | 4 +++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/source/screen.c b/source/screen.c index 0fa9f78..64c1cc3 100644 --- a/source/screen.c +++ b/source/screen.c @@ -2,6 +2,7 @@ #include #include #include +#include /* internal libraries */ #include "vec.h" @@ -103,10 +104,20 @@ static void screen_place_windows(void) { } } +//get the curent directory and copy it into a ref-counted memory block +//return a pointer to the new block +char* pwd(){ + char* dir = getcwd(NULL, 0); + char* rid = mem_allocate(sizeof(char)*strlen(dir), NULL); + strcpy(rid, dir); + free(dir); + return rid; +} + static frame_t* screen_frame_new(void) { frame_t* p_frame = (frame_t*)mem_allocate(sizeof(frame_t),&screen_frame_free); p_frame->p_win = newwin(1, 1, 0, 0); - char* path = state_get_focused_frame() ? state_get_focused_frame()->workdir->path : getcwd(NULL, 0); + char* path = state_get_focused_frame() ? state_get_focused_frame()->workdir->path : pwd(); p_frame->workdir = workdir_new(path); return p_frame; } diff --git a/source/workdir.c b/source/workdir.c index df5b713..6e16149 100644 --- a/source/workdir.c +++ b/source/workdir.c @@ -24,6 +24,7 @@ WorkDir_T* workdir_new(char* path){ WorkDir_T* wd = mem_allocate(sizeof(WorkDir_T), &workdir_free); wd->idx = 0; wd->path = path; + mem_retain(path); wd->vfiles = vec_new(0); workdir_ls(wd); wd->top_index = 0; @@ -33,6 +34,7 @@ WorkDir_T* workdir_new(char* path){ void workdir_free(void* p_wd){ WorkDir_T* wd = (WorkDir_T*)p_wd; mem_release(wd->vfiles); + mem_release(wd->path); } void workdir_next(WorkDir_T* wd) { @@ -93,7 +95,7 @@ char* workdir_cd_down(WorkDir_T* wd){ void workdir_cd(WorkDir_T* wd) { char* newpath = (wd->idx == 0) ? workdir_cd_up(wd) : workdir_cd_down(wd); if(is_dir(newpath)){ - //TODO: this segfaults: mem_release(wd->path); + mem_release(wd->path); wd->path = newpath; wd->idx = 0; wd->top_index = 0; -- 2.54.0