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 59e9908409f5c86cbd4b4598e133fe7033dc7dee
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 15 12:40:50 2026 -0600

    eo_cxx: a connection lets go of its closure when the emitter goes
    
    event_add(event, object, f), and every generated *_event_cb_add behind
    it, never freed its closure unless the caller disconnected: a lambda
    that captured state (win.delete_request_event_cb_add([s]{...}) with s a
    shared_ptr) leaked it forever, even after the window was deleted.
    
    The signal_connection it returned also held a strong reference on the
    emitter, so a forgotten connection kept its object alive.
    
    And the connection was copyable with every copy owning the deleter, so
    disconnecting two copies (or a scoped_signal_connection made from a
    signal_connection that is disconnected too) ran efl_event_callback_del
    and deleted the closure twice, a crash.
    
    New rules:
    - The connection lives as long as the emitter is valid. A second
      registration on the emitter's Efl.Object "invalidate", with the lowest
      priority so it runs after the user handlers of that event, cuts it:
      the callback is unregistered and the closure freed while the object is
      still alive. Cutting at invalidate (not at destruct) matters: a
      closure that captured its own emitter holds a reference, and freeing
      it only at destruct kept parented objects alive past efl_del and
      leaked parentless ones. A connection to "del" or "destruct" is itself
      an end-of-life handler, so it is cut at "destruct", the last event,
      after the user handlers of those.
    - What is given up: once invalidated, an object no longer calls handlers
      connected through event_add for later events, except del and destruct.
    - Two rules for users: a parentless object whose handler captures it by
      value is a cycle (use a wref, or the handler's argument), and a del or
      destruct handler must use its argument, not a captured wrapper of the
      emitter.
    - signal_connection holds no reference on the emitter. Handles share one
      link: any number of copies and scoped handles may disconnect, the
      first wins, the others and a disconnect after the emitter is gone do
      nothing.
    - An emitter that is already invalidating, or invalidated but still
      referenced, takes no new connection except to del or destruct: the
      closure is freed at once and the handle comes back already cut.
      Otherwise such a connection would never be cut and leak, and its
      handler could run after invalidation.
    - A handler that disconnects itself or deletes its emitter while it runs
      keeps its closure for the call.
    
    For out-of-tree users: closures no longer outlive their emitter, and a
    kept connection no longer keeps the object alive; hold the object
    yourself if you relied on that. Disconnecting by hand needs no change
    and is now safe to do twice.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/bindings/cxx/eo_cxx/eo_event.hh             | 228 ++++++++-----
 src/tests/eolian_cxx/eolian_cxx_test_binding.cc | 408 ++++++++++++++++++++++++
 2 files changed, 563 insertions(+), 73 deletions(-)

diff --git a/src/bindings/cxx/eo_cxx/eo_event.hh b/src/bindings/cxx/eo_cxx/eo_event.hh
index f855533110..1198ddf2ee 100644
--- a/src/bindings/cxx/eo_cxx/eo_event.hh
+++ b/src/bindings/cxx/eo_cxx/eo_event.hh
@@ -26,6 +26,7 @@
 #include "eo_concrete.hh"
 #include "eo_cxx_interop.hh"
 
+#include <climits>
 #include <functional>
 #include <memory>
 
@@ -39,27 +40,62 @@ static const callback_priority default_ = 0;
 static const callback_priority  after = 100;
 }
 
+namespace _detail {
+
+/* What every handle on one connection shares. Whoever cuts first (a handle,
+   or the emitter itself being invalidated) runs the cutter once; every later
+   attempt finds nothing to do. It holds no reference on the emitter. Not thread
+   safe, like the rest of the binding: a connection is cut from the thread that
+   runs the main loop. */
+struct connection_link
+{
+  std::function<void()> cutter;
+
+  void cut()
+  {
+    if (!cutter) return;
+    std::function<void()> c;
+    c.swap(cutter);
+    c();
+  }
+};
+
+}
+
+/**
+ * @brief A connection made by @ref event_add.
+ *
+ * The connection lives as long as the emitter is valid: when the emitter is
+ * invalidated the callback registration goes and the closure is freed. A handle
+ * is optional (drop it and nothing changes) and does not keep the emitter
+ * alive. Handles are cheap to copy and any number of them, and of
+ * @ref scoped_signal_connection made from them, may disconnect: the first
+ * one wins, the others, and a disconnect after the emitter is gone, do
+ * nothing.
+ */
 struct signal_connection
 {
   signal_connection(std::function<void()> deleter)
-    : _deleter(deleter) {}
+    : _link(std::make_shared<_detail::connection_link>())
+  {
+    _link->cutter = deleter;
+  }
+  signal_connection(std::shared_ptr<_detail::connection_link> link)
+    : _link(std::move(link)) {}
   void disconnect()
   {
-    if(_deleter)
-      {
-        _deleter();
-        _deleter = std::function<void()>();
-      }
+    if(_link)
+      _link->cut();
   }
 private:
-  std::function<void()> _deleter;
+  std::shared_ptr<_detail::connection_link> _link;
   friend struct scoped_signal_connection;
 };
 
 struct scoped_signal_connection
 {
   scoped_signal_connection(signal_connection const& other)
-    : _deleter(other._deleter)
+    : _link(other._link)
   {
   }
   ~scoped_signal_connection()
@@ -68,74 +104,47 @@ struct scoped_signal_connection
   }
   void disconnect()
   {
-    if(_deleter)
-      {
-        _deleter();
-        _deleter = std::function<void()>();
-      }
+    if(_link)
+      _link->cut();
   }
   scoped_signal_connection(scoped_signal_connection&& other)
-    : _deleter(other._deleter)
+    : _link(std::move(other._link))
   {
-    other._deleter = std::function<void()>();
+    other._link.reset();
   }
 private:
-  std::function<void()> _deleter;
+  std::shared_ptr<_detail::connection_link> _link;
 
   scoped_signal_connection& operator=(scoped_signal_connection const&) = delete;
   scoped_signal_connection(scoped_signal_connection const&) = delete;
 };
 
+namespace _detail {
+
 /* The closure is shared: the registration owns one reference, and every call
    in progress takes another for its duration. A handler may end its own
-   context or disconnect its own handle (the ecore thread-safe call runs
-   immediately on the main thread), and the closure it is executing must
-   survive that. */
+   emitter or disconnect its own handle, and the closure it is executing must
+   survive that. The registration is what both of the Eo callbacks (the
+   handler and the end-of-life hook) receive as data; whoever cuts the
+   connection deletes it. */
 template <typename F>
-struct _event_deleter
+struct registration
 {
-  typedef std::shared_ptr<F> Holder;
-  _event_deleter(Holder* data, Eo* eo, ::Efl_Event_Cb cb, Efl_Event_Description const* description)
-    : _data(data), _eo( ::efl_ref(eo)), _cb(cb), _description(description)
-  {
-  }
-  ~_event_deleter()
-  {
-    ::efl_unref(_eo);
-  }
-  _event_deleter(_event_deleter const& other)
-    : _data(other._data), _eo( ::efl_ref(other._eo)), _cb(other._cb), _description(other._description)
-  {}
-  _event_deleter& operator=(_event_deleter const& other)
-  {
-    ::efl_unref( _eo);
-    _data = other._data;
-    _eo = ::efl_ref(other._eo);
-    _cb = other._cb;
-    _description = other._description;
-    return *this;
-  }
-
-  void operator()() const
-  {
-    ::efl_event_callback_del(_eo, _description, _cb, _data);
-    /* Drops the registration's reference; a call in progress holds its own. */
-    delete _data;
-  }
-
-private:
-  Holder* _data;
-  Eo* _eo;
-  ::Efl_Event_Cb _cb;
-  Efl_Event_Description const* _description;
+  std::shared_ptr<F> closure;
+  std::shared_ptr<connection_link> link;
 };
 
+/* The emitter is going: the connection is cut and its closure freed.
+   Registered with the lowest priority, so every user handler of the same
+   event (priority 0) has run first. Cutting unregisters, which is legal at
+   any point of the teardown. */
 template <typename F>
-signal_connection make_signal_connection(std::unique_ptr<std::shared_ptr<F> >& data, Eo* eo, ::Efl_Event_Cb cb, Efl_Event_Description const* description)
+void emitter_going(void* data, ::Efl_Event const*)
 {
-  signal_connection c(_event_deleter<F>(data.get(), eo, cb, description));
-  data.release();
-  return c;
+   auto link = static_cast<registration<F>*>(data)->link;
+   link->cut();
+}
+
 }
 
 namespace _detail {
@@ -156,32 +165,105 @@ void event_callback(void *data, ::Efl_Event const* event)
 {
    T wrapper(::efl_ref(event->object));
    /* Our own reference keeps the closure alive if the handler severs the
-      connection (or ends its context) while it runs. */
-   std::shared_ptr<F> f = *static_cast<std::shared_ptr<F>*>(data);
+      connection (or deletes its emitter) while it runs. */
+   std::shared_ptr<F> f = static_cast<registration<F>*>(data)->closure;
    _detail::really_call_event<T, P>
      (wrapper, *f, event->info, std::is_void<P>{});
 }
 }
 
+namespace _detail {
+
 template <typename Event, typename Object, typename F>
-signal_connection event_add(Event event, Object object, F&& function)
+std::shared_ptr<connection_link> event_add_link(Event event, Object object, F&& function)
 {
   static_assert((eo::is_eolian_object<Object>::value), "Type is not an object");
 
   typedef typename std::remove_reference<F>::type function_type;
-  std::unique_ptr<std::shared_ptr<function_type> > f
-    (new std::shared_ptr<function_type>(new function_type(std::forward<F>(function))));
+  typedef registration<function_type> registration_type;
+  std::unique_ptr<registration_type> r(new registration_type);
+  r->closure.reset(new function_type(std::forward<F>(function)));
+  r->link = std::make_shared<connection_link>();
 
-  ::efl_event_callback_priority_add
-      (object._eo_ptr(), event.description(), 0
-       , static_cast<Efl_Event_Cb>
-       (&_detail::event_callback<Object, typename Event::parameter_type, function_type>)
-       , f.get());
-  return make_signal_connection
-    (f, object._eo_ptr()
-     , static_cast<Efl_Event_Cb>
-     (&_detail::event_callback<Object, typename Event::parameter_type, function_type>)
-     , event.description());
+  Eo* eo = object._eo_ptr();
+  ::Efl_Event_Cb cb = static_cast<Efl_Event_Cb>
+    (&event_callback<Object, typename Event::parameter_type, function_type>);
+  /* Freed when the emitter is invalidated, i.e. when it loses its parent or
+     its last reference, while it is still alive: a closure that captured its
+     own emitter must not keep it alive past that. A connection to "del" or
+     "destruct" is itself an end-of-life handler and must run, so it is freed
+     at "destruct", the last event. */
+  Efl_Event_Description const* description = event.description();
+  bool late = (description == EFL_EVENT_DEL) || (description == EFL_EVENT_DESTRUCT);
+  /* Past its invalidation (begun, from its own handler, or done, while other
+     references keep it alive) the object calls no handler but del and
+     destruct, and its invalidate event will not be emitted again to cut the
+     connection. So the connection is not made: it comes back already cut and
+     the closure is freed here. efl_invalidating_get stays set from the start
+     of the invalidation; efl_invalidated_get would also answer for an object
+     whose parent only is invalidated, which still gets its own event. */
+  if (!late && ::efl_invalidating_get(eo))
+    return r->link;
+  Efl_Event_Description const* end = late ? EFL_EVENT_DESTRUCT : EFL_EVENT_INVALIDATE;
+  ::Efl_Event_Cb gone = static_cast<Efl_Event_Cb>
+    (&emitter_going<function_type>);
+  registration_type* data = ""
+  std::shared_ptr<connection_link> link = data->link;
+
+  ::efl_event_callback_priority_add(eo, description, 0, cb, data);
+  ::efl_event_callback_priority_add(eo, end, SHRT_MAX, gone, data);
+  link->cutter = [eo, description, cb, end, gone, data]
+    {
+      ::efl_event_callback_del(eo, description, cb, data);
+      ::efl_event_callback_del(eo, end, gone, data);
+      /* Drops the registration's reference; a call in progress holds its own. */
+      delete data;
+    };
+  return link;
+}
+
+}
+
+/**
+ * @brief Call @p function when @p object emits @p event.
+ *
+ * The connection lives as long as @p object is valid: when the emitter is
+ * invalidated (it loses its parent, or its last reference) the registration
+ * is removed and the closure is freed, so a lambda capturing state does not
+ * leak it, and one capturing its own emitter does not keep it alive. The
+ * closure is freed after the user handlers of Efl.Object's @c invalidate
+ * (they run first), and, for a connection to @c del or @c destruct, at the
+ * emitter's @c destruct, after those handlers.
+ *
+ * What is given up: after its invalidation an object no longer calls the
+ * handlers connected through this function, except the ones for @c del and
+ * @c destruct.
+ *
+ * An object that is already invalidating (the call comes from its own
+ * @c invalidate handler) or invalidated but still referenced takes no new
+ * connection but to @c del and @c destruct: the connection is not made, the
+ * returned handle is already cut and the closure is freed at once.
+ *
+ * Two rules follow. An object with no parent whose handler captures it by
+ * value (a strong wrapper) is a cycle: the closure holds a reference, so the
+ * object is not invalidated until the connection is cut; capture an
+ * @c efl::eo::wref, or use the object the handler is given as its first
+ * argument. And a handler of @c del or @c destruct must use its argument, not
+ * a captured wrapper of the emitter, which would be released while the
+ * emitter is being destroyed.
+ *
+ * The returned @ref signal_connection is optional. It does not keep
+ * @p object alive; keep it to cut the connection earlier, from any number of
+ * copies (the first disconnect wins). Disconnecting after the emitter is
+ * gone does nothing.
+ *
+ * A handler may disconnect its own connection, or delete its emitter, while
+ * it runs: the closure outlives the call and is freed after it.
+ */
+template <typename Event, typename Object, typename F>
+signal_connection event_add(Event event, Object object, F&& function)
+{
+  return signal_connection(_detail::event_add_link(event, object, std::forward<F>(function)));
 }
 
 } }
diff --git a/src/tests/eolian_cxx/eolian_cxx_test_binding.cc b/src/tests/eolian_cxx/eolian_cxx_test_binding.cc
index 91109a2b7f..88abde9ecf 100644
--- a/src/tests/eolian_cxx/eolian_cxx_test_binding.cc
+++ b/src/tests/eolian_cxx/eolian_cxx_test_binding.cc
@@ -588,6 +588,402 @@ EFL_START_TEST(eolian_cxx_test_beta_cascading)
 }
 EFL_END_TEST
 
+namespace {
+/* A closure member whose destruction is counted, once per object. */
+struct counted
+{
+  int* destroyed;
+  explicit counted(int* d) : destroyed(d) {}
+  counted(counted const&) = delete;
+  ~counted() { ++*destroyed; }
+};
+}
+
+/* The three-argument form frees its closure when the emitter goes. */
+EFL_START_TEST(eolian_cxx_test_event_closure_freed_with_emitter)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int destroyed = 0, calls = 0;
+  std::unique_ptr<nonamespace::Generic> g
+    (new nonamespace::Generic(efl::eo::instantiate));
+  auto* c = &calls;
+  {
+    auto k = std::make_shared<counted>(&destroyed);
+    efl::eolian::event_add(g->prefix_event1_event, *g,
+                           [c, k] (nonamespace::Generic) { ++*c; });
+  }
+  g->call_event1();
+  ck_assert_int_eq(calls, 1);
+  ck_assert_int_eq(destroyed, 0);
+  g.reset();
+  ck_assert_int_eq(destroyed, 1);
+  ecore_main_loop_iterate();
+  ck_assert_int_eq(destroyed, 1);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+/* A forgotten (kept) connection does not keep the emitter alive. */
+EFL_START_TEST(eolian_cxx_test_event_connection_is_weak)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int destroyed = 0;
+  std::unique_ptr<nonamespace::Generic> g
+    (new nonamespace::Generic(efl::eo::instantiate));
+  Eo* raw = g->_eo_ptr();
+  std::unique_ptr<efl::eolian::signal_connection> cp;
+  {
+    auto k = std::make_shared<counted>(&destroyed);
+    cp.reset(new efl::eolian::signal_connection(efl::eolian::event_add
+      (g->prefix_event1_event, *g, [k] (nonamespace::Generic) {})));
+  }
+  efl::eolian::signal_connection& c = *cp;
+  ck_assert_int_eq(efl_ref_count(raw), 1);   /* the wrapper's, nobody else's */
+  g.reset();                                  /* last reference: it dies now */
+  ck_assert_int_eq(destroyed, 1);
+  c.disconnect();                             /* after the emitter: nothing */
+  ck_assert_int_eq(destroyed, 1);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+/* Disconnecting after the emitter is gone is a no-op (valgrind). */
+EFL_START_TEST(eolian_cxx_test_event_disconnect_after_emitter)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int destroyed = 0;
+  std::unique_ptr<efl::eolian::scoped_signal_connection> scoped;
+  std::unique_ptr<efl::eolian::signal_connection> plain;
+  {
+    nonamespace::Generic g(efl::eo::instantiate);
+    auto k = std::make_shared<counted>(&destroyed);
+    efl::eolian::signal_connection c = efl::eolian::event_add
+      (g.prefix_event1_event, g, [k] (nonamespace::Generic) {});
+    k.reset();
+    plain.reset(new efl::eolian::signal_connection(c));
+    scoped.reset(new efl::eolian::scoped_signal_connection(c));
+  }
+  ck_assert_int_eq(destroyed, 1);
+  plain->disconnect();
+  scoped.reset();
+  plain->disconnect();
+  ck_assert_int_eq(destroyed, 1);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+/* Any number of copies and scoped handles: the first disconnect wins, the
+ * callback is removed once and the closure freed once (valgrind: no double
+ * free). */
+EFL_START_TEST(eolian_cxx_test_event_copies_disconnect_once)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int destroyed = 0, calls = 0;
+  nonamespace::Generic g(efl::eo::instantiate);
+  auto* c = &calls;
+  std::unique_ptr<efl::eolian::signal_connection> c1p;
+  {
+    auto k = std::make_shared<counted>(&destroyed);
+    c1p.reset(new efl::eolian::signal_connection(efl::eolian::event_add
+      (g.prefix_event1_event, g, [c, k] (nonamespace::Generic) { ++*c; })));
+  }
+  efl::eolian::signal_connection& c1 = *c1p;
+  efl::eolian::signal_connection c2 = c1;
+  {
+    efl::eolian::scoped_signal_connection s(c1);
+    efl::eolian::scoped_signal_connection s2(std::move(s));
+    g.call_event1();
+    ck_assert_int_eq(calls, 1);
+    c1.disconnect();
+    ck_assert_int_eq(destroyed, 1);
+    c2.disconnect();
+    s2.disconnect();
+    c1.disconnect();
+    ck_assert_int_eq(destroyed, 1);
+  }                       /* scoped handles die: still nothing */
+  g.call_event1();
+  ck_assert_int_eq(calls, 1);
+  ck_assert_int_eq(destroyed, 1);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+/* A handler of the end-of-life events, attached with event_add, still runs
+ * once while the emitter is torn down, and its closure goes afterwards. */
+EFL_START_TEST(eolian_cxx_test_event_teardown_handlers_still_run)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int del_calls = 0, inv_calls = 0, del_alive = 0, inv_alive = 0;
+  int del_destroyed = 0, inv_destroyed = 0;
+  {
+    nonamespace::Generic g(efl::eo::instantiate);
+    auto* dc = &del_calls;
+    auto* ic = &inv_calls;
+    auto* da = &del_alive;
+    auto* ia = &inv_alive;
+    auto* dd = &del_destroyed;
+    auto* id = &inv_destroyed;
+    auto dk = std::make_shared<counted>(&del_destroyed);
+    auto ik = std::make_shared<counted>(&inv_destroyed);
+    efl::eolian::event_add(efl::Object::del_event, g,
+                           [dc, da, dd, dk] (nonamespace::Generic)
+                           {
+                              ++*dc;
+                              *da += (*dd == 0);
+                           });
+    efl::eolian::event_add(efl::Object::invalidate_event, g,
+                           [ic, ia, id, ik] (nonamespace::Generic)
+                           {
+                              ++*ic;
+                              *ia += (*id == 0);
+                           });
+    dk.reset();
+    ik.reset();
+  }
+  ck_assert_int_eq(inv_calls, 1);
+  ck_assert_int_eq(del_calls, 1);
+  ck_assert_int_eq(inv_alive, 1);   /* the closure was still there for it */
+  ck_assert_int_eq(del_alive, 1);
+  ck_assert_int_eq(inv_destroyed, 1);
+  ck_assert_int_eq(del_destroyed, 1);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+/* A handler that disconnects itself keeps its closure for the call and frees
+ * it after (valgrind). */
+EFL_START_TEST(eolian_cxx_test_event_handler_self_disconnect_frees)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int destroyed = 0, seen = 0;
+  nonamespace::Generic g(efl::eo::instantiate);
+  std::unique_ptr<efl::eolian::signal_connection> h;
+  auto* hp = &h;
+  auto* out = &seen;
+  auto* dp = &destroyed;
+  std::vector<int> payload(16, 7);
+  {
+    auto k = std::make_shared<counted>(&destroyed);
+    h.reset(new efl::eolian::signal_connection(efl::eolian::event_add
+      (g.prefix_event1_event, g,
+       [hp, out, dp, payload, k] (nonamespace::Generic)
+       {
+         (*hp)->disconnect();
+         *out += payload[5] + (*dp == 0);
+       })));
+  }
+  g.call_event1();
+  ck_assert_int_eq(seen, 8);
+  ck_assert_int_eq(destroyed, 1);
+  g.call_event1();
+  ck_assert_int_eq(seen, 8);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+/* A handler that deletes its emitter while it runs (valgrind). */
+EFL_START_TEST(eolian_cxx_test_event_handler_deletes_emitter)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int destroyed = 0, seen = 0;
+  std::unique_ptr<nonamespace::Generic> g
+    (new nonamespace::Generic(efl::eo::instantiate));
+  auto* gp = &g;
+  auto* out = &seen;
+  auto* dp = &destroyed;
+  std::vector<int> payload(16, 7);
+  {
+    auto k = std::make_shared<counted>(&destroyed);
+    efl::eolian::event_add
+      (g->prefix_event1_event, *g,
+       [gp, out, dp, payload, k] (nonamespace::Generic)
+       {
+         gp->reset();                       /* the emitter goes while we run */
+         *out += payload[5] + (*dp == 0);
+       });
+  }
+  g->call_event1();
+  ck_assert_int_eq(seen, 8);
+  ck_assert_int_eq(destroyed, 1);
+  ecore_main_loop_iterate();
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+static int _binding_log_errors = 0;
+
+static void
+_binding_count_errors(const Eina_Log_Domain *d EINA_UNUSED, Eina_Log_Level level,
+                      const char *file EINA_UNUSED, const char *fnc EINA_UNUSED,
+                      int line EINA_UNUSED, const char *fmt EINA_UNUSED,
+                      void *data EINA_UNUSED, va_list args EINA_UNUSED)
+{
+  if (level <= EINA_LOG_LEVEL_ERR) _binding_log_errors++;
+}
+
+/* A handler that captured its own emitter, which has a parent: deleting the
+ * object invalidates it, its connections are cut then, the closure goes and
+ * gives back the reference it held, so the object is destroyed (no leak) and
+ * nothing is logged as an error (no "too many unrefs"). */
+EFL_START_TEST(eolian_cxx_test_event_self_capture_parented)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int destroyed = 0, calls = 0;
+  nonamespace::Generic parent(efl::eo::instantiate);
+  eina_log_print_cb_set(_binding_count_errors, nullptr);
+  _binding_log_errors = 0;
+  {
+    nonamespace::Generic g(efl::eo::instantiate, parent);
+    auto k = std::make_shared<counted>(&destroyed);
+    auto* c = &calls;
+    efl::eolian::event_add(g.prefix_event1_event, g,
+                           [g, c, k] (nonamespace::Generic) { ++*c; });
+    k.reset();
+    g.call_event1();
+    ck_assert_int_eq(calls, 1);
+    ck_assert_int_eq(destroyed, 0);
+    ::efl_del(g._eo_ptr());   /* parent gone: invalidate cuts the closure */
+  }
+  ck_assert_int_eq(destroyed, 1);
+  ecore_main_loop_iterate();
+  eina_log_print_cb_set(eina_log_print_cb_stderr, nullptr);
+  ck_assert_int_eq(_binding_log_errors, 0);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+/* A parentless object whose handler captures it strongly is a cycle, and that
+ * is documented: it lives until the connection is cut, and goes with it. */
+EFL_START_TEST(eolian_cxx_test_event_self_capture_parentless_cycle)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int destroyed = 0;
+  eina_log_print_cb_set(_binding_count_errors, nullptr);
+  _binding_log_errors = 0;
+  std::unique_ptr<efl::eolian::signal_connection> handle;
+  {
+    nonamespace::Generic g(efl::eo::instantiate);
+    auto k = std::make_shared<counted>(&destroyed);
+    handle.reset(new efl::eolian::signal_connection(efl::eolian::event_add
+      (g.prefix_event1_event, g, [g, k] (nonamespace::Generic) {})));
+  }                                /* the last wrapper but the closure's */
+  ck_assert_int_eq(destroyed, 0);  /* still alive: that is the cycle */
+  handle->disconnect();
+  ck_assert_int_eq(destroyed, 1);  /* cut: closure freed, object released */
+  eina_log_print_cb_set(eina_log_print_cb_stderr, nullptr);
+  ck_assert_int_eq(_binding_log_errors, 0);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+/* A del handler that uses its argument is called once, and no error is
+ * logged while the emitter is torn down. */
+EFL_START_TEST(eolian_cxx_test_event_del_handler_uses_argument)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int calls = 0, destroyed = 0, same = 0;
+  eina_log_print_cb_set(_binding_count_errors, nullptr);
+  _binding_log_errors = 0;
+  {
+    nonamespace::Generic g(efl::eo::instantiate);
+    Eo* raw = g._eo_ptr();
+    auto k = std::make_shared<counted>(&destroyed);
+    auto* c = &calls;
+    auto* sm = &same;
+    efl::eolian::event_add(efl::Object::del_event, g,
+                           [c, sm, raw, k] (nonamespace::Generic obj)
+                           {
+                              ++*c;
+                              *sm += (obj._eo_ptr() == raw);
+                           });
+  }
+  ck_assert_int_eq(calls, 1);
+  ck_assert_int_eq(same, 1);
+  ck_assert_int_eq(destroyed, 1);
+  eina_log_print_cb_set(eina_log_print_cb_stderr, nullptr);
+  ck_assert_int_eq(_binding_log_errors, 0);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+/* A connection made while the emitter is invalidating (from its own
+ * invalidate handler) or already invalidated but still alive is not made: the
+ * object calls no such handler after its invalidation, so there is nothing to
+ * keep, and the closure is freed at once. Disconnecting later, after the
+ * emitter is gone, logs nothing. */
+EFL_START_TEST(eolian_cxx_test_event_connect_while_invalidating)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int destroyed = 0, calls = 0;
+  eina_log_print_cb_set(_binding_count_errors, nullptr);
+  _binding_log_errors = 0;
+  {
+    std::unique_ptr<nonamespace::Generic> g
+      (new nonamespace::Generic(efl::eo::instantiate));
+    auto* c = &calls;
+    auto* d = &destroyed;
+    efl::eolian::event_add(efl::Object::invalidate_event, *g,
+      [c, d] (nonamespace::Generic obj)
+      {
+        auto k = std::make_shared<counted>(d);
+        efl::eolian::event_add(obj.prefix_event1_event, obj,
+                               [c, k] (nonamespace::Generic) { ++*c; });
+        ck_assert_int_eq(*d, 0);   /* k still held here */
+      });
+    g.reset();
+  }
+  ck_assert_int_eq(destroyed, 1);
+  ck_assert_int_eq(calls, 0);
+  eina_log_print_cb_set(eina_log_print_cb_stderr, nullptr);
+  ck_assert_int_eq(_binding_log_errors, 0);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
+EFL_START_TEST(eolian_cxx_test_event_connect_after_invalidate)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int destroyed = 0, calls = 0;
+  eina_log_print_cb_set(_binding_count_errors, nullptr);
+  _binding_log_errors = 0;
+  nonamespace::Generic parent(efl::eo::instantiate);
+  std::unique_ptr<efl::eolian::signal_connection> handle;
+  {
+    nonamespace::Generic g(efl::eo::instantiate, parent);
+    Eo* raw = g._eo_ptr();
+    ::efl_ref(raw);
+    ::efl_del(raw);               /* invalidated, kept alive by the others */
+    auto k = std::make_shared<counted>(&destroyed);
+    auto* c = &calls;
+    handle.reset(new efl::eolian::signal_connection(efl::eolian::event_add
+      (g.prefix_event1_event, g, [c, k] (nonamespace::Generic) { ++*c; })));
+    k.reset();
+    ck_assert_int_eq(destroyed, 1);   /* not made: closure already freed */
+    g.call_event1();
+    ck_assert_int_eq(calls, 0);
+    ::efl_unref(raw);
+  }                                   /* g's own reference: it dies now */
+  ecore_main_loop_iterate();
+  handle->disconnect();               /* after the emitter: nothing, no error */
+  handle.reset();
+  eina_log_print_cb_set(eina_log_print_cb_stderr, nullptr);
+  ck_assert_int_eq(_binding_log_errors, 0);
+  ecore_shutdown();
+}
+EFL_END_TEST
+
 /* A "string" event carries a char const* (Efl.Ui.Textbox's activated carries
  * the text that way), not a string_view. */
 EFL_START_TEST(eolian_cxx_test_string_event_payload)
@@ -636,6 +1032,18 @@ void
 eolian_cxx_test_binding(TCase* tc)
 {
    tcase_add_test(tc, eolian_cxx_test_event_handler_disconnects_own_handle);
+   tcase_add_test(tc, eolian_cxx_test_event_closure_freed_with_emitter);
+   tcase_add_test(tc, eolian_cxx_test_event_connection_is_weak);
+   tcase_add_test(tc, eolian_cxx_test_event_disconnect_after_emitter);
+   tcase_add_test(tc, eolian_cxx_test_event_copies_disconnect_once);
+   tcase_add_test(tc, eolian_cxx_test_event_teardown_handlers_still_run);
+   tcase_add_test(tc, eolian_cxx_test_event_handler_self_disconnect_frees);
+   tcase_add_test(tc, eolian_cxx_test_event_handler_deletes_emitter);
+   tcase_add_test(tc, eolian_cxx_test_event_self_capture_parented);
+   tcase_add_test(tc, eolian_cxx_test_event_self_capture_parentless_cycle);
+   tcase_add_test(tc, eolian_cxx_test_event_del_handler_uses_argument);
+   tcase_add_test(tc, eolian_cxx_test_event_connect_while_invalidating);
+   tcase_add_test(tc, eolian_cxx_test_event_connect_after_invalidate);
    tcase_add_test(tc, eolian_cxx_test_string_event_payload);
    tcase_add_test(tc, eolian_cxx_test_string_arrays);
    tcase_add_test(tc, eolian_cxx_test_binding_constructor_only_required);

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

Reply via email to