[FIX] remove memory leaks 14/19614/1
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Wed, 16 Apr 2014 16:07:55 +0000 (20:07 +0400)
committerVitaliy Cherepanov <v.cherepanov@samsung.com>
Wed, 16 Apr 2014 16:07:55 +0000 (20:07 +0400)
add memory debug system and fix old memory leak

Change-Id: Idfe22b4ac76a8a1c23101361d6ce2a1758e1c05c
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
daemon/Makefile
daemon/da_inst.c
daemon/da_protocol.c
daemon/debug.h
daemon/main.c

index 7f93393..de606d2 100644 (file)
@@ -2,6 +2,7 @@ CC :=gcc
 DEBUG_CPPFLAGS =                               \
                -DDEBUG                                 \
                -DUSE_LOG_ONCE                  \
+#              -DMALLOC_DEBUG_ON               \#
 #              -DDEB_PRINTBUF                  \#
 #              -DPARSE_DEBUG_ON                \#
 #              -DTHREAD_SAMPLING_DEBUG \#
@@ -42,7 +43,8 @@ DAEMON_SRCS =                 \
        device_vconf.c          \
        device_system_info.c    \
        device_camera.c         \
-       smack.c
+       smack.c                                 \
+       malloc_debug.c
 
 DAEMON_OBJS = $(patsubst %.c,%.o, $(DAEMON_SRCS))
 
index 8b93617..a058cdf 100644 (file)
@@ -107,6 +107,7 @@ static void free_probe_element(struct probe_list_t *probe)
 
 static void free_data_element(struct data_list_t *lib)
 {
+       free(lib->data);
        free(lib);
 }
 
@@ -128,8 +129,9 @@ static void free_data(struct data_list_t *lib)
 
 void free_data_list(struct data_list_t **data)
 {
+       struct data_list_t *next;
        while (*data != NULL) {
-               struct data_list_t *next = (*data)->next;
+               next = (*data)->next;
                free_data(*data);
                *data = next;
        }
@@ -468,7 +470,6 @@ static char *pack_data_list_to_array(struct data_list_t *list, uint32_t *len, ui
                res = malloc(size);
                to = res;
                if (to != NULL) {
-                       memset(to, '*', size);
                        pack_int32(to, cnt);
                        for (p = list; p != NULL; p = p->next)
                                to = pack_data_to_array(p, to, pack);
@@ -533,6 +534,9 @@ static int generate_msg(struct msg_t **msg, struct lib_list_t *lib_list, struct
                app = app->next;
        }
 
+       free(packed_lib_list);
+       free(packed_app_list);
+
        // print_buf((char *)*msg, size, "ANSWER");
        return 1;
 }
@@ -657,3 +661,29 @@ int msg_swap_inst_remove(struct msg_buf_t *data, struct user_space_inst_t *us_in
        *err = ERR_NO;
        return 0;
 }
+
+void msg_swap_free_all_data(struct user_space_inst_t *us_inst)
+{
+       LOGI("new_lib_inst_list %p\n", new_lib_inst_list);
+       if (new_lib_inst_list != NULL) {
+               LOGI("free new_lib_inst_list start\n");
+               free_data_list(&new_lib_inst_list);
+               new_lib_inst_list = NULL;
+               LOGI("free new_lib_inst_list finish\n");
+       }
+
+       LOGI("us_inst->lib_inst_list %p\n", us_inst->lib_inst_list);
+       if (us_inst->lib_inst_list != NULL) {
+               LOGI("free us_inst->lib_inst_list start\n");
+               free_data_list(&us_inst->lib_inst_list);
+               us_inst->lib_inst_list = NULL;
+               LOGI("free us_isnt->lib_inst_list finish\n");
+       }
+
+       LOGI("us_inst->app_inst_list %p\n", us_inst->app_inst_list);
+       if (us_inst->app_inst_list != NULL) {
+               LOGI("free us_inst->app_inst_list start\n");
+               free_data_list(&us_inst->app_inst_list);
+               LOGI("free us_inst->app_isnt_list finish\n");
+       }
+}
index 8804f74..4a37cd4 100644 (file)
@@ -851,7 +851,7 @@ static void get_serialized_time(uint32_t dst[2])
 static int process_msg_start(struct msg_buf_t *msg_control)
 {
        enum ErrorCode err_code = ERR_CANNOT_START_PROFILING;
-       struct msg_t *msg_reply;
+       struct msg_t *msg_reply = NULL;
        uint32_t serialized_time[2];
 
        if (check_running_status(&prof_session) == 1) {
@@ -901,7 +901,8 @@ send_ack:
        get_serialized_time(serialized_time);
        sendACKToHost(NMSG_START, err_code, (void *)&serialized_time,
                      sizeof(serialized_time));
-
+       if (msg_reply != NULL)
+               free(msg_reply);
        return -(err_code != ERR_NO);
 }
 
index 1278a8d..fbf0e54 100644 (file)
@@ -34,6 +34,7 @@
 #include <stdarg.h>
 
 #include "da_protocol.h"
+#include "malloc_debug.h"
 #include "utils.h"
 
 #ifdef __cplusplus
index a304837..2b073ac 100644 (file)
@@ -451,6 +451,12 @@ int main()
 
        close_system_file_descriptors();
 
+       //DO NOT USE THIS FUNCTION FOR RELEASE IT IS TOO SLOW
+#ifdef MALLOC_DEBUG_ON
+       msg_swap_free_all_data(&prof_session.user_space_inst);
+#endif
+
        LOGI("main finished\n");
+       print_malloc_list(NULL, 0);
        return 0;
 }