Skip to content

Commit

Permalink
Store member variable createConnectedElement by value
Browse files Browse the repository at this point in the history
Was stored as pointer before. This change solves potentially dangling pointers after object goes out-of-scope.
  • Loading branch information
svengoldberg committed Jan 20, 2025
1 parent b31ae2f commit 3414751
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion TIGLViewer/src/ModificatorContainerWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void ModificatorContainerWidget::setSectionModificator(QList<tigl::CTiglSectionE
}


void ModificatorContainerWidget::setSectionsModificator(Ui::ElementModificatorInterface& element)
void ModificatorContainerWidget::setSectionsModificator(const Ui::ElementModificatorInterface& element)
{
hideAllSpecializedWidgets();
ui->sectionsModificator->setCreateConnectedElement(element);
Expand Down
2 changes: 1 addition & 1 deletion TIGLViewer/src/ModificatorContainerWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public slots:
void setFuselagesModificator(tigl::CCPACSFuselages& fuselages);
void setElementModificator(tigl::CTiglSectionElement& element);
void setSectionModificator(QList<tigl::CTiglSectionElement*> elements);
void setSectionsModificator(Ui::ElementModificatorInterface& conntedElementI);
void setSectionsModificator(Ui::ElementModificatorInterface const& conntedElementI);
// for positioning, we need two different calls for wing and for fuselage. Otherwise, we miss to invalidate the
// associated wing or fuselage
void setPositioningModificator(tigl::CCPACSWing& wing, tigl::CCPACSPositioning& positioning);
Expand Down
2 changes: 1 addition & 1 deletion TIGLViewer/src/ModificatorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void ModificatorManager::dispatch(cpcr::CPACSTreeItem* item)
};

auto element = get_element_interface(typePtr);
modificatorContainerWidget->setSectionsModificator(element);
modificatorContainerWidget->setSectionsModificator(std::move(element));
}
else if (item->getType() == "positioning" ) {
tigl::CTiglUIDManager& uidManager = doc->GetConfiguration().GetUIDManager();
Expand Down
9 changes: 4 additions & 5 deletions TIGLViewer/src/modificators/ModificatorSectionsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ ModificatorSectionsWidget::ModificatorSectionsWidget(QWidget* parent)
, ui(new Ui::ModificatorSectionsWidget)
{
ui->setupUi(this);
createConnectedElement = nullptr;
connect(ui->addConnectedElementBtn, SIGNAL(pressed()), this, SLOT(execNewConnectedElementDialog()));
connect(ui->deleteConnectedElementBtn, SIGNAL(pressed()), this, SLOT(execDeleteConnectedElementDialog()));
}
Expand All @@ -40,14 +39,14 @@ ModificatorSectionsWidget::~ModificatorSectionsWidget()
delete ui;
}

void ModificatorSectionsWidget::setCreateConnectedElement(Ui::ElementModificatorInterface& element)
void ModificatorSectionsWidget::setCreateConnectedElement(Ui::ElementModificatorInterface const& element)
{
createConnectedElement = &element;
createConnectedElement = element;
}

void ModificatorSectionsWidget::execNewConnectedElementDialog()
{
if (createConnectedElement == nullptr) {
if (createConnectedElement == std::nullopt) {
LOG(ERROR) << "ModificatorSectionsWidget:: is not correctly set!";
return;
}
Expand Down Expand Up @@ -88,7 +87,7 @@ void ModificatorSectionsWidget::execNewConnectedElementDialog()
void ModificatorSectionsWidget::execDeleteConnectedElementDialog()
{

if (createConnectedElement == nullptr) {
if (createConnectedElement == std::nullopt) {
LOG(ERROR) << "ModificatorWingsWidget::execDeleteWingDialog: wings is not set!";
return;
}
Expand Down
6 changes: 4 additions & 2 deletions TIGLViewer/src/modificators/ModificatorSectionsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <QWidget>
#include <string>
#include <optional>
#include "CCPACSFuselage.h"
#include "CCPACSWing.h"

Expand Down Expand Up @@ -77,11 +78,12 @@ public slots:
explicit ModificatorSectionsWidget(QWidget* parent = nullptr);
~ModificatorSectionsWidget();

void setCreateConnectedElement(Ui::ElementModificatorInterface& element);
void setCreateConnectedElement(Ui::ElementModificatorInterface const& element);

private:
Ui::ModificatorSectionsWidget* ui;
Ui::ElementModificatorInterface* createConnectedElement;
// std::optional used here to account for empty initializiation of member variable in construtor (ptr and nullptr used before)
std::optional<Ui::ElementModificatorInterface> createConnectedElement;
};

#endif // MODIFICATORSECTIONSWIDGET_H

0 comments on commit 3414751

Please sign in to comment.