]> git.mdlowis.com Git - archive/afm.git/commitdiff
fix memory leaks
authora bellenir <a@bellenir.com>
Sat, 26 Jul 2014 01:01:11 +0000 (01:01 +0000)
committera bellenir <a@bellenir.com>
Sat, 26 Jul 2014 01:01:11 +0000 (01:01 +0000)
source/screen.c
source/workdir.c

index 0fa9f78cbbdf69e0d56a3ec24f08bd11f03aa5cc..64c1cc3456ccf12f74d77ba8468e5ac5db1646d8 100644 (file)
@@ -2,6 +2,7 @@
 #include <ncurses.h>
 #include <unistd.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 /* 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;
 }
index df5b713649acf4779d624bbffbffd0b9e11e6413..6e161492e62fcf8490f51dacba028ca23e6f81cd 100644 (file)
@@ -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;