Skip to content

Commit

Permalink
[TD]use weak_ptr as deletion guard
Browse files Browse the repository at this point in the history
- prevent crash if dimension deleted by Python while dialog
  is open
  • Loading branch information
WandererFan committed Mar 17, 2024
1 parent 2326d52 commit b4e11db
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
33 changes: 29 additions & 4 deletions src/Mod/TechDraw/Gui/TaskDimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ using namespace TechDraw;
using namespace TechDrawGui;

TaskDimension::TaskDimension(QGIViewDimension *parent, ViewProviderDimension *dimensionVP) :
ui(new Ui_TaskDimension)
ui(new Ui_TaskDimension),
m_parent(parent),
m_dimensionVP(dimensionVP)
{
m_parent = parent;
m_dimensionVP = dimensionVP;

ui->setupUi(this);

// Tolerancing
Expand Down Expand Up @@ -143,6 +142,11 @@ TaskDimension::~TaskDimension()

bool TaskDimension::accept()
{
if (m_dimensionVP.expired()) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Missing Dimension"),
QObject::tr("Dimension not found. Was it deleted? Can not continue."));
return true;
}
Gui::Document* doc = m_dimensionVP->getDocument();
m_dimensionVP->getObject()->purgeTouched();
doc->commitCommand();
Expand All @@ -153,6 +157,11 @@ bool TaskDimension::accept()

bool TaskDimension::reject()
{
if (m_dimensionVP.expired()) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Missing Dimension"),
QObject::tr("Dimension not found. Was it deleted? Can not continue."));
return true;
}
Gui::Document* doc = m_dimensionVP->getDocument();
doc->abortCommand();
recomputeFeature();
Expand All @@ -165,6 +174,10 @@ bool TaskDimension::reject()

void TaskDimension::recomputeFeature()
{
if (m_dimensionVP.expired()) {
// guard against deletion while this dialog is running
return;
}
App::DocumentObject* objVP = m_dimensionVP->getObject();
assert(objVP);
objVP->getDocument()->recomputeFeature(objVP);
Expand Down Expand Up @@ -297,12 +310,18 @@ void TaskDimension::onArbitraryTolerancesChanged()

void TaskDimension::onFlipArrowheadsChanged()
{
if (m_dimensionVP.expired()) {
return;
}
m_dimensionVP->FlipArrowheads.setValue(ui->cbArrowheads->isChecked());
recomputeFeature();
}

void TaskDimension::onColorChanged()
{
if (m_dimensionVP.expired()) {
return;
}
App::Color ac;
ac.setValue<QColor>(ui->dimensionColor->color());
m_dimensionVP->Color.setValue(ac);
Expand All @@ -311,12 +330,18 @@ void TaskDimension::onColorChanged()

void TaskDimension::onFontsizeChanged()
{
if (m_dimensionVP.expired()) {
return;
}
m_dimensionVP->Fontsize.setValue(ui->qsbFontSize->value().getValue());
recomputeFeature();
}

void TaskDimension::onDrawingStyleChanged()
{
if (m_dimensionVP.expired()) {
return;
}
m_dimensionVP->StandardAndStyle.setValue(ui->comboDrawingStyle->currentIndex());
recomputeFeature();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Mod/TechDraw/Gui/TaskDimension.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#ifndef GUI_TASKVIEW_TASKDIMENSION_H
#define GUI_TASKVIEW_TASKDIMENSION_H

#include <Gui/DocumentObserver.h>
#include <Gui/TaskView/TaskDialog.h>
#include <Gui/TaskView/TaskView.h>
#include <Mod/TechDraw/TechDrawGlobal.h>
Expand Down Expand Up @@ -72,7 +73,7 @@ private Q_SLOTS:
private:
std::unique_ptr<Ui_TaskDimension> ui;
QGIViewDimension *m_parent;
ViewProviderDimension *m_dimensionVP;
Gui::WeakPtrT<ViewProviderDimension> m_dimensionVP;
std::pair<double, bool> getAngleFromSelection();
};

Expand Down

0 comments on commit b4e11db

Please sign in to comment.