Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Studio: avoid lambda functions for signal callbacks #2294

Merged
merged 1 commit into from Nov 15, 2021

Conversation

rodolforg
Copy link
Contributor

Specially important for WorkArea queuing rendering.

Reasoning by Kjell Ahlstedt (gtkmm maintainer) follows below:

From the documentation of sigc::mem_fun():

 * @note If the object type inherits from sigc::trackable, and the
 * functor returned from mem_fun() is assigned to a sigc::slot, the functor
 * will be automatically cleared when the object goes out of scope. Invoking
 * that slot will then have no effect and will not try to use the destroyed
 * instance.

In most examples in the gtkmm tutorial the signal handlers are members of widgets.
All widgets inherit from sigc::trackable. The automatic disconnection when a
widget is destroyed would be lost if a sigc::mem_fun() is replaced by a C++ lambda
expression. Unless it's combined with sigc::track_obj():

m_button.signal_clicked().connect(sigc::track_obj([this]{ on_button_clicked(); }, *this));

but that's no better than sigc::mem_fun(), is it?

This kind of automatic disconnection is not always necessary. Some instances of
mem_fun() could be replaced by lambda expressions, to show that it's possible.
Preferably together with a description of the pros and cons of mem_fun() and
lambda expressions. But I don't recommend replacing the majority of the mem_fun()s.

https://gitlab.gnome.org/GNOME/gtkmm-documentation/-/issues/10

@rodolforg
Copy link
Contributor Author

The changes in WorkArea can fix https://forums.synfig.org/t/synfig-dev-v1-5-0-is-crashing/12480 .

A while ago I had some crashes when changing the selected duck (or when unselecting them all) and I tracked down to this problem. I forgot to make a PR.

Specially important for WorkArea queuing rendering.

Reasoning by Kjell Ahlstedt (gtkmm maintainer) follows below:

From the documentation of sigc::mem_fun():

```
 * @note If the object type inherits from sigc::trackable, and the
 * functor returned from mem_fun() is assigned to a sigc::slot, the functor
 * will be automatically cleared when the object goes out of scope. Invoking
 * that slot will then have no effect and will not try to use the destroyed
 * instance.
```

In most examples in the gtkmm tutorial the signal handlers are members of widgets.
All widgets inherit from sigc::trackable. The automatic disconnection when a
widget is destroyed would be lost if a sigc::mem_fun() is replaced by a C++ lambda
expression. Unless it's combined with sigc::track_obj():

```c++
m_button.signal_clicked().connect(sigc::track_obj([this]{ on_button_clicked(); }, *this));
```
but that's no better than sigc::mem_fun(), is it?

This kind of automatic disconnection is not always necessary. Some instances of
mem_fun() could be replaced by lambda expressions, to show that it's possible.
Preferably together with a description of the pros and cons of mem_fun() and
lambda expressions. But I don't recommend replacing the majority of the mem_fun()s.

https://gitlab.gnome.org/GNOME/gtkmm-documentation/-/issues/10
@ice0 ice0 merged commit 055e0f9 into synfig:master Nov 15, 2021
@ice0
Copy link
Collaborator

ice0 commented Nov 15, 2021

Merged. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants