Skip to content

Commit

Permalink
add a comment
Browse files Browse the repository at this point in the history
  • Loading branch information
gfgtdf committed Jun 30, 2018
1 parent a82c925 commit 459fd5f
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/gui/dialogs/multiplayer/mp_join_game.cpp
Expand Up @@ -456,6 +456,12 @@ void mp_join_game::generate_side_list(window& window)
//
const auto handler = [this, side_num](bool& handled, bool& halt) {
show_flg_select(side_num);
// note: this function is called from a std::function object stored in the widget
// and show_flg_select which internally calls
// show_dialog -> pump -> ... -> network_handler -> ... -> generate_side_list
// might destroy that std::function object while it is executed, this means that
// using the captured variables 'this' and 'side_num' after this will result in
// unexpected behaviour or crashes.
handled = halt = true;
};

Expand Down

2 comments on commit 459fd5f

@Vultraz
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this could be solved in C++17 by using this-by-copy capture (*this - or tmp = *this for C++14)?

@gfgtdf
Copy link
Contributor Author

@gfgtdf gfgtdf commented on 459fd5f Jul 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no this would do something very differnt, it would copy the mp_join_game object. The way labdas basicially works is is as a shortcut for normal function objects, meaning this code

int a; 
int b;
auto c=[a,&b](int c){ return a+b+c;}

is basicially equivalent to

int a; 
int b;
struct anon_function_type_1
{
  int a_;
  int& b_;
  auto operator()(int c) { return a_ + b_ + c;}
};
auto c = anon_function_type_1{a_, b_};

changing it to 'this-by-copy' would be equivalent to changing the type of the memper varible of the functionstruct from mp_join_game * to mp_join_game but acessing freed mamory is still ub no matter whether its of type mp_join_game * or mp_join_game

Please sign in to comment.