From 1590606c0dcd24cdf7a49a098181150a203cf5ec 2017-09-27 12:51:46 From: Alexandre Leroux Date: 2017-09-27 12:51:46 Subject: [PATCH] Fixes crashes on release when duplicating/renaming a variable --- diff --git a/gui/src/Variable/VariableInspectorWidget.cpp b/gui/src/Variable/VariableInspectorWidget.cpp index 365b843..038bcdb 100644 --- a/gui/src/Variable/VariableInspectorWidget.cpp +++ b/gui/src/Variable/VariableInspectorWidget.cpp @@ -184,22 +184,29 @@ void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept if (selectedVariables.size() == 1) { auto selectedVariable = selectedVariables.front(); - auto duplicateFun = [&selectedVariable]() { - sqpApp->variableController().cloneVariable(selectedVariable); + auto duplicateFun = [varW = std::weak_ptr(selectedVariable)]() + { + if (auto var = varW.lock()) { + sqpApp->variableController().cloneVariable(var); + } }; tableMenu.addAction(tr("Duplicate"), duplicateFun); - auto renameFun = [&selectedVariable, &model, this]() { - // Generates forbidden names (names associated to existing variables) - auto allVariables = model->variables(); - auto forbiddenNames = QVector(allVariables.size()); - std::transform(allVariables.cbegin(), allVariables.cend(), forbiddenNames.begin(), - [](const auto &variable) { return variable->name(); }); - - RenameVariableDialog dialog{selectedVariable->name(), forbiddenNames, this}; - if (dialog.exec() == QDialog::Accepted) { - selectedVariable->setName(dialog.name()); + auto renameFun = [ varW = std::weak_ptr(selectedVariable), &model, this ]() + { + if (auto var = varW.lock()) { + // Generates forbidden names (names associated to existing variables) + auto allVariables = model->variables(); + auto forbiddenNames = QVector(allVariables.size()); + std::transform(allVariables.cbegin(), allVariables.cend(), + forbiddenNames.begin(), + [](const auto &variable) { return variable->name(); }); + + RenameVariableDialog dialog{var->name(), forbiddenNames, this}; + if (dialog.exec() == QDialog::Accepted) { + var->setName(dialog.name()); + } } };