Fix memory management issues 16/301816/1 accepted/tizen_6.0_unified tizen_6.0 accepted/tizen/6.0/unified/20231127.150710
authorArtur Świgoń <a.swigon@samsung.com>
Thu, 9 Nov 2023 15:41:42 +0000 (16:41 +0100)
committerArtur Świgoń <a.swigon@samsung.com>
Thu, 23 Nov 2023 12:15:27 +0000 (13:15 +0100)
Change-Id: Ic276eb376e9d5246c3f366c6c57449e47cfce840

src/window_tracker.c

index 1b980811eb9cec5bbed16968eb2516461112745f..0221c80d434fec469d9eebbb7a594875575131bc 100644 (file)
@@ -195,6 +195,11 @@ static void _window_stack_print(WindowTrackerData *wtd)
 static void _window_append(WindowTrackerData *wtd, AtspiAccessible *window, Eina_Bool view_change_need,
                                                   Eina_Bool keyboard_window_is, WindowActivateInfoType window_activate_info_type)
 {
+       // Increase reference count immediately to guard against g_object_unref() in the loop below.
+       // This is a danger only if 'window == wi->window', which is a stricter condition than
+       // atspi_accessible_is_equal(window, wi->window), which can be true even if 'window != wi->window'.
+       g_object_ref(window);
+
        _purge_windows_list(wtd);
        /* Remove window first if it exists already */
        WindowInfo *wi;
@@ -212,9 +217,10 @@ static void _window_append(WindowTrackerData *wtd, AtspiAccessible *window, Eina
        wi = g_malloc0(sizeof(WindowInfo));
        if (!wi) {
                DEBUG("Memory allocation by g_malloc0 is failed");
+               g_object_unref(window);
                return;
        }
-       wi->window = g_object_ref(window);
+       wi->window = window;
        wi->keyboard_window_is = keyboard_window_is;
        wi->view_change_need = view_change_need;
        wi->window_activate_info_type = window_activate_info_type;
@@ -319,8 +325,11 @@ static void _window_remove(WindowTrackerData *wtd, AtspiAccessible *window)
                                gchar *id = atspi_accessible_get_unique_id(wi->window, NULL);
                                DEBUG("Check upper window: %s, keyboard window: %d", id, wi->keyboard_window_is);
                                g_free(id);
-                               if (wi->keyboard_window_is)
+                               if (wi->keyboard_window_is) {
                                        wtd->window_infos = eina_list_remove(wtd->window_infos, wi);
+                                       g_object_unref(wi->window);
+                                       g_free(wi);
+                               }
                        } else
                                DEBUG("Current window info list is emtpy");
                }