Callbacks and captures

button->on_activate
    ([main_window=x::make_weak_capture(main_window)]
     (const x::w::callback_trigger_t &trigger, const auto &ignore)
     {
          main_winow.get([&]
                         (const auto &main_window)
                         {
                              // ...
                         });
     });

As explained in the section called “Special rules for captured references in callbacks”, callbacks attached to display elements can't capture references to parent or child display elements. This creates an internal circular reference that prevents the display elements, which are reference-counted objects, from getting destroyed.

But the application's action in response to a click on a lowly button typically involves doing work that often affects the entire application and the entire display window.

The solution is to use x::make_weak_capture(), from the core LibCXX classes, to capture a weak pointer. The callback attempts to get() a regular reference from it, and proceeds with its business if this succeeds. If the callback happens to execute while the main window is getting destroyed, and the top level main_window no longer exists, get() fails to recover a strong reference, and the callback bails out, and lets the execution thread finish cleaning things up.

In gridlayoutmanager.C each button's on_activate() takes this approach. Strictly speaking, gridlayoutmanager.C doesn't actually need to do this. Each button's action affects only other display elements that are neither its parent or child display element; gridlayoutmanager.C uses weak captures only to give an example of using this design pattern.