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 e8577394b1f27644bd1d5d56b041359f4739c5b6
Author: Cedric BAIL <[email protected]>
AuthorDate: Thu Nov 12 04:19:54 2026 -0600

    cxx: a handler may disconnect its own handle and keep running
    
    ecore_main_loop_thread_safe_call_async runs at once on the main thread,
    so the "deferred" deletion of the closure ran while the handler was
    still executing it; a handler that disconnected its own handle (a Close
    button closing its window) then read freed captures.
    
    The closure is now a shared_ptr held by the registration; each call
    takes its own reference for its duration. The false "deferred" comments
    are corrected.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/bindings/cxx/eo_cxx/eo_event.hh             | 27 ++++++++++++---------
 src/tests/eolian_cxx/eolian_cxx_test_binding.cc | 31 +++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 11 deletions(-)

diff --git a/src/bindings/cxx/eo_cxx/eo_event.hh b/src/bindings/cxx/eo_cxx/eo_event.hh
index 0053a0f5b3..f855533110 100644
--- a/src/bindings/cxx/eo_cxx/eo_event.hh
+++ b/src/bindings/cxx/eo_cxx/eo_event.hh
@@ -86,10 +86,16 @@ private:
   scoped_signal_connection(scoped_signal_connection const&) = delete;
 };
 
+/* 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. */
 template <typename F>
 struct _event_deleter
 {
-  _event_deleter(F* data, Eo* eo, ::Efl_Event_Cb cb, Efl_Event_Description const* description)
+  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)
   {
   }
@@ -113,23 +119,19 @@ struct _event_deleter
   void operator()() const
   {
     ::efl_event_callback_del(_eo, _description, _cb, _data);
-    ::ecore_main_loop_thread_safe_call_async(&_deleter_call, _data);
+    /* Drops the registration's reference; a call in progress holds its own. */
+    delete _data;
   }
 
 private:
-  static void _deleter_call(void* data)
-  {
-    delete static_cast<F*>(data);
-  }
-
-  F* _data;
+  Holder* _data;
   Eo* _eo;
   ::Efl_Event_Cb _cb;
   Efl_Event_Description const* _description;
 };
 
 template <typename F>
-signal_connection make_signal_connection(std::unique_ptr<F>& data, Eo* eo, ::Efl_Event_Cb cb, Efl_Event_Description const* description)
+signal_connection make_signal_connection(std::unique_ptr<std::shared_ptr<F> >& data, Eo* eo, ::Efl_Event_Cb cb, Efl_Event_Description const* description)
 {
   signal_connection c(_event_deleter<F>(data.get(), eo, cb, description));
   data.release();
@@ -153,7 +155,9 @@ template <typename T, typename P, typename F>
 void event_callback(void *data, ::Efl_Event const* event)
 {
    T wrapper(::efl_ref(event->object));
-   F *f = static_cast<F*>(data);
+   /* 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);
    _detail::really_call_event<T, P>
      (wrapper, *f, event->info, std::is_void<P>{});
 }
@@ -165,7 +169,8 @@ signal_connection event_add(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<function_type> f(new function_type(std::forward<F>(function)));
+  std::unique_ptr<std::shared_ptr<function_type> > f
+    (new std::shared_ptr<function_type>(new function_type(std::forward<F>(function))));
 
   ::efl_event_callback_priority_add
       (object._eo_ptr(), event.description(), 0
diff --git a/src/tests/eolian_cxx/eolian_cxx_test_binding.cc b/src/tests/eolian_cxx/eolian_cxx_test_binding.cc
index 47c4bc1840..91109a2b7f 100644
--- a/src/tests/eolian_cxx/eolian_cxx_test_binding.cc
+++ b/src/tests/eolian_cxx/eolian_cxx_test_binding.cc
@@ -20,6 +20,8 @@
 #include <Ecore.h>
 
 #include <map>
+#include <vector>
+#include <memory>
 
 #define GENERIC_BETA
 #define EOLIANCXXTEST_API
@@ -602,9 +604,38 @@ EFL_START_TEST(eolian_cxx_test_string_event_payload)
 }
 EFL_END_TEST
 
+/* A handler may disconnect its own handle and then go on using what it
+ * captured: the closure outlives the call (valgrind: no invalid read). */
+EFL_START_TEST(eolian_cxx_test_event_handler_disconnects_own_handle)
+{
+  efl::eo::eo_init i;
+  ecore_init();
+  int seen = 0;
+  nonamespace::Generic g(efl::eo::instantiate);
+  std::unique_ptr<efl::eolian::scoped_signal_connection> handle;
+  std::vector<int> payload(16, 7);
+  auto* h = &handle;
+  auto* out = &seen;
+  handle.reset(new efl::eolian::scoped_signal_connection(
+    efl::eolian::event_add(g.prefix_event1_event, g,
+                           [h, out, payload] (nonamespace::Generic)
+                           {
+                             h->reset();
+                             *out += payload[5];
+                           })));
+  g.call_event1();
+  ck_assert_int_eq(seen, 7);
+  g.call_event1();
+  ck_assert_int_eq(seen, 7);
+  ecore_main_loop_iterate();
+  ecore_shutdown();
+}
+EFL_END_TEST
+
 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_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