Fix memory leak of secure-erase UI 31/147731/2
authors414kim <s414.kim@samsung.com>
Tue, 5 Sep 2017 10:21:34 +0000 (19:21 +0900)
committers414kim <s414.kim@samsung.com>
Fri, 8 Sep 2017 06:46:55 +0000 (15:46 +0900)
. free the memory allocated for notification and app_control handle.

Change-Id: I35ff9b7a4bd56c74805d61a70f3154ced8b46785
Signed-off-by: s414kim <s414.kim@samsung.com>
tools/apps/secure-erase/include/secure-erase.h
tools/apps/secure-erase/src/main.c
tools/apps/secure-erase/src/ui.c

index 3ed8777..ef7b1a8 100644 (file)
@@ -25,6 +25,7 @@
 #include <app_common.h>
 #include <bundle.h>
 #include <dlog.h>
+#include <notification.h>
 #include <Elementary.h>
 #include <efl_extension.h>
 #include <system_settings.h>
 
 #define __(str) dgettext("secure-erase", str)
 
-void create_base_window(void);
+struct ui_data {
+       Evas_Object *window;
+       notification_h notification;
+       app_control_h appcontrol;
+       Ecore_Event_Handler *ecore_handler;
+};
+
+void create_base_window(struct ui_data *ud);
 
 Evas_Object *ui_create_window(const char *package);
 Evas_Object *ui_create_popup(Evas_Object *parent, const char *title, const char *body);
index e129bf2..dfef3f9 100644 (file)
@@ -31,11 +31,33 @@ static void app_resume(void *data)
 
 static void app_terminate(void *data)
 {
+       struct ui_data *ud = (struct ui_data *)data;
+       if (ud->window) {
+               evas_object_del(ud->window);
+               ud->window = NULL;
+       }
+
+       if (ud->notification) {
+               notification_delete(ud->notification);
+               notification_free(ud->notification);
+               ud->notification = NULL;
+       }
+
+       if (ud->appcontrol) {
+               app_control_destroy(ud->appcontrol);
+               ud->appcontrol = NULL;
+       }
+
+       if (ud->ecore_handler) {
+               ecore_event_handler_del(ud->ecore_handler);
+               ud->ecore_handler = NULL;
+       }
 }
 
 static void app_control(app_control_h app_control, void *data)
 {
-       create_base_window();
+       struct ui_data *ud = (struct ui_data *)data;
+       create_base_window(ud);
        return;
 }
 
@@ -59,6 +81,7 @@ int main(int argc, char *argv[])
 {
        int ret = 0;
        app_event_handler_h handlers[5] = {NULL, };
+       struct ui_data ud = {0, };
 
        ui_app_lifecycle_callback_s event_callback = {0, };
 
@@ -70,7 +93,7 @@ int main(int argc, char *argv[])
 
        ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, ui_app_lang_changed, NULL);
 
-       ret = ui_app_main(argc, argv, &event_callback, NULL);
+       ret = ui_app_main(argc, argv, &event_callback, &ud);
        if (ret != APP_ERROR_NONE)
                dlog_print(DLOG_ERROR, LOG_TAG, "ui_app_main is failed. err = %d", ret);
 
index 29e1e83..218a566 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ *
  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  */
 #include <app_control_internal.h>
 #include <vconf.h>
-#include <notification.h>
 #include <ode/secure-erase.h>
 #include "secure-erase.h"
 
 static const char *device_path = "/opt/usr"; /*To be removed */
-static Evas_Object *window = NULL;
 
 static notification_h add_notification(notification_type_e type, const char *title, const char *content, const char *image, notification_ly_type_e layout)
 {
@@ -62,32 +61,30 @@ static notification_h add_notification(notification_type_e type, const char *tit
        return handle;
 }
 
-static int create_finish_notification(void)
+
+static int create_finish_notification(struct ui_data *ud)
 {
        int ret = 0;
-       notification_h handle = NULL;
-
-       handle = add_notification(NOTIFICATION_TYPE_NOTI, __("IDS_ST_HEADER_SECURE_ERASE_ABB"),
+       ud->notification = add_notification(NOTIFICATION_TYPE_NOTI, __("IDS_ST_HEADER_SECURE_ERASE_ABB"),
                                                          __("IDS_ST_BUTTON_DONE"), APP_ICON_PATH, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
-       if (handle == NULL) {
+       if (ud->notification == NULL) {
                return -1;
        }
 
-       ret = notification_set_display_applist(handle, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY | NOTIFICATION_DISPLAY_APP_LOCK);
+       ret = notification_set_display_applist(ud->notification, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY | NOTIFICATION_DISPLAY_APP_LOCK);
        if (ret != NOTIFICATION_ERROR_NONE) {
                return -1;
        }
 
-       ret = notification_post(handle);
+       ret = notification_post(ud->notification);
        if (ret != NOTIFICATION_ERROR_NONE) {
                return -1;
        }
 
-       ret = notification_free(handle);
+       ret = notification_free(ud->notification);
        if (ret != NOTIFICATION_ERROR_NONE) {
                return -1;
        }
-
        return 0;
 }
 
@@ -96,7 +93,6 @@ static void delete_notification(notification_h handle)
        if (handle == NULL) {
                return;
        }
-
        notification_delete(handle);
        notification_free(handle);
        handle = NULL;
@@ -105,85 +101,81 @@ static void delete_notification(notification_h handle)
 
 static Eina_Bool progressbar_timer_cb(void *data)
 {
-       notification_h handle = NULL;
+       struct ui_data *ud = (struct ui_data *)data;
        char *progress = NULL;
        int percentage = 0;
        double current_progress = 0.0, noti_progress = 0.0;
 
-       handle = notification_load_by_tag("secure-erase-noti");
-       if (handle == NULL) {
-               return ECORE_CALLBACK_DONE;
-       }
-
        progress = vconf_get_str(VCONFKEY_ODE_ERASE_PROGRESS);
        if (progress) {
                percentage = atoi(progress);
        } else {
-               delete_notification(handle);
+               delete_notification(ud->notification);
                ui_app_exit();
                return ECORE_CALLBACK_DONE;
        }
 
        if (percentage == 100) {
-               delete_notification(handle);
-               create_finish_notification();
+               delete_notification(ud->notification);
+               int ret = 0;
+               ret = create_finish_notification(ud);
+               if (ret < 0 && ud->notification != NULL) {
+                       delete_notification(ud->notification);
+               }
                ui_app_exit();
                return ECORE_CALLBACK_DONE;
-       } else if (percentage < 100) {
-               current_progress = (double)(percentage / 100.0);
-               notification_get_progress(handle, &noti_progress);
-               if (current_progress != noti_progress) {
-                       notification_set_progress(handle, (double)(percentage / 100.0));
-                       notification_update(handle);
-               }
        }
 
+       current_progress = (double)(percentage / 100.0);
+       notification_get_progress(ud->notification, &noti_progress);
+       if (current_progress != noti_progress) {
+               notification_set_progress(ud->notification, (double)(percentage / 100.0));
+               notification_update(ud->notification);
+       }
        return ECORE_CALLBACK_PASS_ON;
 }
 
-static int create_progress_notification(void)
+static int create_progress_notification(struct ui_data *ud)
 {
        int ret = 0;
-       notification_h handle = NULL;
 
-       handle = add_notification(NOTIFICATION_TYPE_ONGOING, __("IDS_ST_HEADER_SECURE_ERASE_ABB"), __("IDS_ST_SBODY_ERASING_ALL_DELETED_DATA_ING_ABB"),
+       ud->notification = add_notification(NOTIFICATION_TYPE_ONGOING, __("IDS_ST_HEADER_SECURE_ERASE_ABB"), __("IDS_ST_SBODY_ERASING_ALL_DELETED_DATA_ING_ABB"),
                                                          APP_ICON_PATH, NOTIFICATION_LY_ONGOING_PROGRESS);
-       if (handle == NULL) {
+       if (ud->notification == NULL) {
                return -1;
        }
 
-       ret = notification_set_progress(handle, 0);
+       ret = notification_set_progress(ud->notification, 0);
        if (ret != NOTIFICATION_ERROR_NONE) {
                return ret;
        }
 
-       ret = notification_set_tag(handle, "secure-erase-noti");
+       ret = notification_set_display_applist(ud->notification, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
        if (ret != NOTIFICATION_ERROR_NONE) {
                return ret;
        }
 
-       ret = notification_set_display_applist(handle, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
+       ret = notification_post(ud->notification);
        if (ret != NOTIFICATION_ERROR_NONE) {
                return ret;
        }
 
-       ret = notification_post(handle);
-       if (ret != NOTIFICATION_ERROR_NONE) {
-               return ret;
-       }
-
-       ecore_timer_add(1, progressbar_timer_cb, NULL);
+       ecore_timer_add(1, progressbar_timer_cb, ud);
        return ret;
 }
 
-static int on_secure_erase(void)
+static int on_secure_erase(struct ui_data *ud)
 {
        int ret = 0;
 
-       evas_object_del(window);
+       evas_object_del(ud->window);
+       ud->window = NULL;
 
-       ret = create_progress_notification();
+       ret = create_progress_notification(ud);
        if (ret != 0) {
+               if (ud->notification) {
+                       delete_notification(ud->notification);
+               }
                dlog_print(DLOG_ERROR, LOG_TAG, "failed to create notification");
                return -1;
        }
@@ -199,6 +191,7 @@ static int on_secure_erase(void)
 static void reply_password_enter_event_cb(app_control_h ug, app_control_h reply, app_control_result_e result, void *data)
 {
        char *result_string = NULL;
+       struct ui_data *ud = (struct ui_data *)data;
 
        if (result != APP_CONTROL_RESULT_SUCCEEDED) {
                dlog_print(DLOG_ERROR, LOG_TAG, "failed to launch setting-pasword");
@@ -212,13 +205,15 @@ static void reply_password_enter_event_cb(app_control_h ug, app_control_h reply,
        }
 
        if (!strcmp(result_string, "Cancel")) {
+               free(result_string);
                return;
        }
 
        if (!strcmp(result_string, "SETTING_PW_TYPE_ENTER_LOCK_TYPE") ||
                        !strcmp(result_string, "SETTING_PW_TYPE_VERIFY_FP_ALT_PASSWORD")) {
                free(result_string);
-               int ret = on_secure_erase();
+               int ret = 0;
+               ret = on_secure_erase(ud);
                if (ret != 0) {
                        dlog_print(DLOG_ERROR, LOG_TAG, "failed to clean the device");
                        ui_app_exit();
@@ -226,40 +221,40 @@ static void reply_password_enter_event_cb(app_control_h ug, app_control_h reply,
        }
 }
 
-static int launch_setting_password(void)
+static int launch_setting_password(struct ui_data *ud)
 {
        int ret = 0;
-       app_control_h app_handler = NULL;
 
-       ret = app_control_create(&app_handler);
+       ret = app_control_create(&ud->appcontrol);
        if (ret != APP_CONTROL_ERROR_NONE) {
                return -1;
        }
 
-       ret = app_control_add_extra_data(app_handler, APP_CONTROL_DATA_SHIFT_WINDOW, "true");
+       ret = app_control_add_extra_data(ud->appcontrol, APP_CONTROL_DATA_SHIFT_WINDOW, "true");
        if (ret != APP_CONTROL_ERROR_NONE) {
                return -1;
        }
 
-       ret = app_control_add_extra_data(app_handler, "viewtype", "SETTING_PW_TYPE_ENTER_LOCK_TYPE");
+       ret = app_control_add_extra_data(ud->appcontrol, "viewtype", "SETTING_PW_TYPE_ENTER_LOCK_TYPE");
        if (ret != APP_CONTROL_ERROR_NONE) {
                return -1;
        }
 
-       ret = app_control_set_app_id(app_handler, "org.tizen.setting-password");
+       ret = app_control_set_app_id(ud->appcontrol, "org.tizen.setting-password");
        if (ret != APP_CONTROL_ERROR_NONE) {
                return -1;
        }
 
-       ret = app_control_send_launch_request(app_handler, reply_password_enter_event_cb, NULL);
+       ret = app_control_send_launch_request(ud->appcontrol, reply_password_enter_event_cb, ud);
        if (ret != APP_CONTROL_ERROR_NONE) {
                return -1;
        }
 
-       ret = app_control_destroy(app_handler);
+       ret = app_control_destroy(ud->appcontrol);
        if (ret != APP_CONTROL_ERROR_NONE) {
                return -1;
        }
+       ud->appcontrol = NULL;
 
        return 0;
 }
@@ -274,23 +269,28 @@ static void secure_erase_next_cb(void *data, Evas_Object *obj, void *event_info)
 {
        int lock_type = 0;
        int ret = 0;
-       Ecore_Event_Handler *ecore_handler = (Ecore_Event_Handler *)data;
+       struct ui_data *ud = (struct ui_data *)data;
+
+       ecore_event_handler_del(ud->ecore_handler);
+       ud->ecore_handler = NULL;
 
-       ecore_event_handler_del(ecore_handler);
        /* Is password set? */
        vconf_get_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &lock_type);
        if (lock_type == SETTING_SCREEN_LOCK_TYPE_PASSWORD
                        || lock_type == SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD) {
-               ret = launch_setting_password();
+               ret = launch_setting_password(ud);
+               if (ret < 0 && ud->appcontrol != NULL) {
+                       app_control_destroy(ud->appcontrol);
+                       ud->appcontrol = NULL;
+               }
        } else {
-               ret = on_secure_erase();
+               ret = on_secure_erase(ud);
        }
 
        if (ret != 0) {
                dlog_print(DLOG_ERROR, LOG_TAG, "fail to clean device");
                ui_app_exit();
        }
-
        return;
 }
 
@@ -305,19 +305,18 @@ static Eina_Bool key_event_cb(void *data, int type, void *event)
        return EINA_TRUE;
 }
 
-void create_base_window(void)
+void create_base_window(struct ui_data *ud)
 {
        Evas_Object *popup = NULL, *cancel_button = NULL, *next_button = NULL;
-       Ecore_Event_Handler *ecore_handler = NULL;
 
        /* Create main UI widget */
-       window = ui_create_window(PACKAGE);
-       popup = ui_create_popup(window, __("IDS_ST_HEADER_SECURE_ERASE_ABB"),
+       ud->window = ui_create_window(PACKAGE);
+       popup = ui_create_popup(ud->window, __("IDS_ST_HEADER_SECURE_ERASE_ABB"),
                                                        __("IDS_ST_POP_ALL_DELETED_DATA_ON_THE_PHONE_WILL_BE_ERASED_THIS_DATA_WILL_NOT_BE_RECOVERABLE_BY_ANYONE_MSG"));
 
-       ecore_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, key_event_cb, NULL);
+       ud->ecore_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, key_event_cb, NULL);
        cancel_button = ui_create_popup_button(popup, _("IDS_ST_BUTTON_CANCEL_ABB"), secure_erase_cancel_cb, NULL);
-       next_button = ui_create_popup_button(popup, _("IDS_ST_BUTTON_ERASE_ABB"), secure_erase_next_cb, ecore_handler);
+       next_button = ui_create_popup_button(popup, _("IDS_ST_BUTTON_ERASE_ABB"), secure_erase_next_cb, ud);
 
        elm_object_part_content_set(popup, "button1", cancel_button);
        elm_object_part_content_set(popup, "button2", next_button);