Fix memory leak, handle hash table size 58/245558/7
authorShinwoo Kim <cinoo.kim@samsung.com>
Mon, 12 Oct 2020 09:17:29 +0000 (18:17 +0900)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 16 Oct 2020 14:47:34 +0000 (16:47 +0200)
(1) Fix memory leak of atspi_accessible_get_reading_material.
(2) Remove defunct accessible from application hash table.
  In case of chromium-efl, it emits lots of "defunct" events without
  "RemoveAccessible", and this makes application hash table too big
  which could cause a Memory Alert on TV product.
(3) If AT-server does not emit "RemoveAccessible", then there is not
  a chance to remove accessible from the application hash table. And
  then the memory usage of AT-client grows bigger and bigger. So this
  patch is using newly defined value HASH_TABLE_SIZE_MAX.

Change-Id: If60d1fa1714d8bbf73b48cf16b1e1274f5a30cb9

atspi/atspi-accessible.c
atspi/atspi-event-listener-private.h
atspi/atspi-event-listener.c
atspi/atspi-misc.c

index c2df8ab..138b73e 100644 (file)
@@ -640,6 +640,8 @@ atspi_accessible_get_reading_material (AtspiAccessible *obj, GError **error)
   parent =  _atspi_dbus_return_accessible_from_iter (&iter);
   reading_material->described_by_accessible = parent;
 
+  dbus_message_unref(reply);
+
   return reading_material;
 }
 
index 5a1a872..41abdfe 100644 (file)
@@ -38,6 +38,8 @@ void _atspi_send_event (AtspiEvent *e);
 
 DBusHandlerResult _atspi_dbus_handle_event (DBusConnection *bus, DBusMessage *message, void *data);
 
+void remove_accessible_from_app_hash (const char *app_name, AtspiAccessible *a);
+
 void
 _atspi_reregister_event_listeners ();
 
index 3d4a8fb..859f4a5 100644 (file)
@@ -1122,6 +1122,11 @@ _atspi_dbus_handle_event (DBusConnection *bus, DBusMessage *message, void *data)
   else if (!strncmp (e.type, "object:state-changed", 20))
   {
     cache_process_state_changed (&e);
+    if (detail && !strncmp(detail, "defunct", 7))
+    {
+      remove_accessible_from_app_hash(sender, e.source);
+      remove_accessible_from_app_hash(sender, e.sender);
+    }
   }
   else if (!strncmp (e.type, "focus", 5))
   {
index 12135e3..fccd502 100644 (file)
@@ -36,6 +36,9 @@
 #include <stdio.h>
 #include <string.h>
 
+/* This value is not fixed, could be changed. */
+#define HASH_TABLE_SIZE_MAX 1000
+
 static void handle_get_items (DBusPendingCall *pending, void *user_data);
 
 static DBusConnection *bus = NULL;
@@ -300,6 +303,13 @@ ref_accessible (const char *app_name, const char *path)
   a = _atspi_accessible_new (app, path);
   if (!a)
     return NULL;
+
+  if (g_hash_table_size (app->hash) > HASH_TABLE_SIZE_MAX)
+  {
+     g_warning ("Accessible hash table is bigger than MAX: %d\n", HASH_TABLE_SIZE_MAX);
+     g_hash_table_remove_all(app->hash);
+  }
+
   g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
   return a;
 }
@@ -368,6 +378,21 @@ handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_
   return DBUS_HANDLER_RESULT_HANDLED;
 }
 
+void
+remove_accessible_from_app_hash (const char *app_name, AtspiAccessible *a)
+{
+  AtspiApplication *app;
+
+  if (!a) return;
+
+  app = get_application (app_name);
+  if (!app) return;
+
+  if (!g_hash_table_lookup (app->hash, a->parent.path)) return;
+
+  g_hash_table_remove (app->hash, a->parent.path);
+}
+
 static DBusHandlerResult
 handle_name_owner_changed (DBusConnection *bus, DBusMessage *message, void *user_data)
 {