This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository efl.

View the commit online.

commit 9b5d7eb77c4f6f2656e50b7069b7188a59e274e6
Author: Cedric BAIL <[email protected]>
AuthorDate: Tue Nov 10 18:06:35 2026 -0600

    elementary: Exact_Model frees what it keeps when it is destroyed
    
    The model had no destructor: the compressed lists of widths and
    heights, the buffers of its three slots and the compressed block of
    zeros that fills the gaps between blocks were never freed. The block of
    zeros is shared by every model, so it is counted and let go of with the
    last list that holds it; like the models, the count is only touched
    from the main loop, so it is neither atomic nor locked.
    
    The new test sets sizes in several blocks, reusing slots and filling
    gaps, then destroys the model. It checks nothing by itself: run under
    valgrind, the definite losses from the model are gone (18 records
    before, none after).
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/lib/elementary/efl_ui_exact_model.c  | 59 ++++++++++++++++++++++++++++++--
 src/lib/elementary/efl_ui_exact_model.eo |  1 +
 src/tests/elementary/efl_ui_model.c      | 49 ++++++++++++++++++++++++++
 3 files changed, 106 insertions(+), 3 deletions(-)

diff --git a/src/lib/elementary/efl_ui_exact_model.c b/src/lib/elementary/efl_ui_exact_model.c
index 3ba2d0e894..3912c3bb5f 100644
--- a/src/lib/elementary/efl_ui_exact_model.c
+++ b/src/lib/elementary/efl_ui_exact_model.c
@@ -54,14 +54,30 @@ _efl_ui_exact_model_efl_object_constructor(Eo *obj, Efl_Ui_Exact_Model_Data *pd)
    return efl_constructor(efl_super(obj, EFL_UI_EXACT_MODEL_CLASS));
 }
 
+// The compressed block of zeros that fills the gaps is shared by every model,
+// and is let go of with the last list that holds it. The count of the lists
+// holding it is not atomic and not locked: it is only ever touched from the
+// main loop, as the models themselves are.
+static Eina_Binbuf *_zero_block = NULL;
+static unsigned int _zero_block_users = 0;
+
+static void
+_efl_ui_exact_model_zero_unref(void)
+{
+   if (--_zero_block_users) return;
+   eina_binbuf_free(_zero_block);
+   _zero_block = NULL;
+}
+
 static Eina_List *
 _efl_ui_exact_model_slot_compress(unsigned int list_index, Eina_List *compressed, unsigned int *buffer)
 {
-   static Eina_Binbuf *z = NULL;
+   Eina_Binbuf *z;
    Eina_Binbuf *cbuf;
    Eina_Binbuf *tbuf;
    Eina_List *l = NULL;
 
+   z = _zero_block;
    l = eina_list_nth_list(compressed, list_index);
 
    tbuf = eina_binbuf_manage_new((unsigned char *) buffer, EFL_UI_EXACT_MODEL_CONTENT_LENGTH, EINA_TRUE);
@@ -77,6 +93,7 @@ _efl_ui_exact_model_slot_compress(unsigned int list_index, Eina_List *compressed
         tbuf = eina_list_data_get(l);
         eina_list_data_set(l, cbuf);
         if (tbuf != z) eina_binbuf_free(tbuf);
+        else _efl_ui_exact_model_zero_unref();
         return compressed;
      }
 
@@ -103,7 +120,7 @@ _efl_ui_exact_model_slot_compress(unsigned int list_index, Eina_List *compressed
                   return compressed;
                }
 
-             z = emile_compress(tbuf, EMILE_LZ4, EMILE_COMPRESSOR_FAST);
+             z = _zero_block = emile_compress(tbuf, EMILE_LZ4, EMILE_COMPRESSOR_FAST);
 
              eina_binbuf_free(tbuf);
              free(zmem);
@@ -116,12 +133,48 @@ _efl_ui_exact_model_slot_compress(unsigned int list_index, Eina_List *compressed
 
         // Fill the list all the way to the needed index with buffer full of zero
         while (eina_list_count(compressed) < list_index)
-          compressed = eina_list_append(compressed, z);
+          {
+             compressed = eina_list_append(compressed, z);
+             _zero_block_users++;
+          }
      }
 
    return eina_list_append(compressed, cbuf);
 }
 
+static void
+_efl_ui_exact_model_compressed_free(Eina_List *list)
+{
+   Eina_Binbuf *b;
+
+   EINA_LIST_FREE(list, b)
+     {
+        if (b == _zero_block) _efl_ui_exact_model_zero_unref();
+        else eina_binbuf_free(b);
+     }
+}
+
+static void
+_efl_ui_exact_model_efl_object_destructor(Eo *obj, Efl_Ui_Exact_Model_Data *pd)
+{
+   unsigned int i;
+
+   // (only the root keeps sizes, its rows have none)
+   _efl_ui_exact_model_compressed_free(pd->compressed.width);
+   _efl_ui_exact_model_compressed_free(pd->compressed.height);
+   pd->compressed.width = NULL;
+   pd->compressed.height = NULL;
+   for (i = 0; i < EINA_C_ARRAY_LENGTH(pd->slot); i++)
+     {
+        free(pd->slot[i].width);
+        free(pd->slot[i].height);
+        pd->slot[i].width = NULL;
+        pd->slot[i].height = NULL;
+     }
+
+   efl_destructor(efl_super(obj, EFL_UI_EXACT_MODEL_CLASS));
+}
+
 static unsigned int *
 _efl_ui_exact_model_buffer_expand(unsigned int list_index, unsigned int *buffer, Eina_List *list)
 {
diff --git a/src/lib/elementary/efl_ui_exact_model.eo b/src/lib/elementary/efl_ui_exact_model.eo
index 0e63976e54..f42517d43d 100644
--- a/src/lib/elementary/efl_ui_exact_model.eo
+++ b/src/lib/elementary/efl_ui_exact_model.eo
@@ -12,6 +12,7 @@ class @beta Efl.Ui.Exact_Model extends Efl.Ui.Size_Model
 
    implements {
       Efl.Object.constructor;
+      Efl.Object.destructor;
       Efl.Model.property { set; get; }
    }
 }
diff --git a/src/tests/elementary/efl_ui_model.c b/src/tests/elementary/efl_ui_model.c
index 3954dd932d..fbc9f97057 100644
--- a/src/tests/elementary/efl_ui_model.c
+++ b/src/tests/elementary/efl_ui_model.c
@@ -416,6 +416,54 @@ EFL_START_TEST(efl_ui_exact_model_many_rows)
 }
 EFL_END_TEST
 
+// An Exact_Model that held sizes in several blocks is let go of: what it kept
+// compressed, the buffers of its slots and the block of zeros it fills the gaps
+// with are its to free (there is nothing to check here but valgrind). The last
+// block is written first so that the blocks before it are filled with zeros,
+// and each of the first ones is visited again after the slots were reused.
+EFL_START_TEST(efl_ui_exact_model_destroyed)
+{
+   Efl_Generic_Model *base_model;
+   Efl_Model *model;
+   Many_Rows *many = calloc(1, sizeof (Many_Rows));
+   static const unsigned int order[] = { 3400, 2100, 1100, 50, 3401, 2101, 1101, 51, 3402 };
+   unsigned int i, loops;
+
+   base_model = efl_add_ref(EFL_GENERIC_MODEL_CLASS, efl_main_loop_get());
+   for (i = 0; i < MANY_ROWS; i++)
+     {
+        efl_model_child_add(base_model);
+     }
+   model = efl_add_ref(EFL_UI_EXACT_MODEL_CLASS, efl_main_loop_get(),
+                       efl_ui_view_model_set(efl_added, base_model));
+   ck_assert(!!model);
+
+   eina_future_then(efl_model_children_slice_get(model, 0, MANY_ROWS), _many_rows_then, many, NULL);
+   for (loops = 0; (loops < 50) && (many->count < MANY_ROWS); loops++)
+     {
+        ecore_main_loop_iterate();
+     }
+   ck_assert_uint_eq(many->count, MANY_ROWS);
+
+   for (i = 0; i < EINA_C_ARRAY_LENGTH(order); i++)
+     {
+        Eina_Future *f = efl_model_property_set(many->rows[order[i]], "self.height",
+                                                eina_value_uint_new(10 + i));
+
+        efl_future_then(many->rows[order[i]], f);
+        ck_assert_uint_eq(_property_uint_expected(many->rows[order[i]], "self.height"), 10 + i);
+     }
+
+   for (i = 0; i < MANY_ROWS; i++)
+     {
+        efl_unref(many->rows[i]);
+     }
+   free(many);
+   efl_unref(model);
+   efl_unref(base_model);
+}
+EFL_END_TEST
+
 void
 efl_ui_model(TCase *tc)
 {
@@ -428,4 +476,5 @@ efl_ui_model(TCase *tc)
    tcase_add_test(tc, efl_ui_average_model_test);
    tcase_add_test(tc, efl_ui_exact_model_test);
    tcase_add_test(tc, efl_ui_exact_model_many_rows);
+   tcase_add_test(tc, efl_ui_exact_model_destroyed);
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to