From e6ea52bc42b52e277cfe3bf3da49ad09c4b8dd50 Mon Sep 17 00:00:00 2001 From: Marian Ohligs Date: Thu, 4 Oct 2012 17:20:38 +0200 Subject: [PATCH] remove memory leak --- kernel/tasks.c | 54 ++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/kernel/tasks.c b/kernel/tasks.c index 8be9ea1d..e8979504 100644 --- a/kernel/tasks.c +++ b/kernel/tasks.c @@ -200,6 +200,32 @@ static void NORETURN do_exit(int arg) { task_t* curr_task = per_core(current_task); uint32_t flags, core_id, fd, status; + if(curr_task->fildes_table) { + for (fd = 0; fd < NR_OPEN; fd++) { + if(curr_task->fildes_table[fd] != NULL) { + /* + * delete a descriptor from the per-process object + * reference table. If this is not the last reference to the underlying + * object, the object will be ignored. + */ + if (curr_task->fildes_table[fd]->count == 1) { + /* try to close the file */ + status = close_fs(curr_task->fildes_table[fd]); + /* close command failed -> return check = errno */ + if (BUILTIN_EXPECT(status < 0, 0)) + kprintf("Task %u was not able to close file descriptor %i. close_fs returned %d", curr_task->id, fd, -status); + kfree(curr_task->fildes_table[fd], sizeof(fildes_t)); + curr_task->fildes_table[fd] = NULL; + } else { + curr_task->fildes_table[fd]->count--; + curr_task->fildes_table[fd] = NULL; + } + } + } + //finally the table has to be cleared. + kfree(curr_task->fildes_table, sizeof(filp_t)*NR_OPEN); + } + kprintf("Terminate task: %u, return value %d\n", curr_task->id, arg); wakeup_blocked_tasks(arg); @@ -214,34 +240,6 @@ static void NORETURN do_exit(int arg) { kfree((void*) tmp, sizeof(vma_t)); } - for (fd = 0; fd < NR_OPEN; fd++) { - if (curr_task->fildes_table[fd] != NULL) { - /* - * delete a descriptor from the per-process object - * reference table. If this is not the last reference to the underlying - * object, the object will be ignored. - */ - if (curr_task->fildes_table[fd]->count == 1) { - // kprintf("CLOSE_FS\n"); - /* try to close the file */ - // status = close_fs(curr_task->fildes_table[fd]); - /* close command failed -> return check = errno */ - if (BUILTIN_EXPECT(status < 0, 0)) - kprintf("Task %u was not able to close file descriptor %i. close_fs returned %d", curr_task->id, fd, -status); - // kfree(curr_task->fildes_table[fd], sizeof(fildes_t)); - // curr_task->fildes_table[fd] = NULL; - } else { - // kprintf("DECREASE REF\n"); - // curr_task->fildes_table[fd]->count--; - // curr_task->fildes_table[fd] = NULL; - } - } - } - - // after closing all file decriptors and cleaning up, the table has to be cleared. - if(!curr_task->fildes_table) - kfree(curr_task->fildes_table, sizeof(filp_t)*NR_OPEN); - spinlock_unlock(&curr_task->vma_lock); drop_pgd(); // delete page directory and its page tables