user1899020
user1899020

Reputation: 13575

Qt connect crash when using lambda function

Code is here

void A::fun()
{
    QAction* act = new QAction(this);
    QAction* act2 = new QAction(this);
    connect(act, QAction::triggered, [this, &act2]() {...; act2.setDisable(true);}
                              // crash when &act2 is used to capture the variable
                              // but it is okay using act2 to capture the variable
}

What is the reason? Thanks.

Upvotes: 0

Views: 554

Answers (1)

cmannett85
cmannett85

Reputation: 22366

You are taking a reference of act2 even though it is a pointer that will go out scope, that is why copying the pointer works.

Upvotes: 3

Related Questions