[TD] avoid memory leaks by using shared_ptr

TD geometry objects are sometimes double deleted.  This
change uses shared_ptr instead of raw pointers to manage
deletions.
This commit is contained in:
Wanderer Fan
2022-01-02 10:12:56 -05:00
committed by WandererFan
parent 4c9191d489
commit e91cc8e329
34 changed files with 290 additions and 279 deletions

View File

@@ -156,8 +156,7 @@ void CmdTechDrawPageDefault::activated(int iMsg)
else {
Base::Console().Log("INFO - Template: %s for Page: %s NOT Found\n", PageName.c_str(), TemplateName.c_str());
}
}
else {
} else {
QMessageBox::critical(Gui::getMainWindow(),
QLatin1String("No template"),
QLatin1String("No default template found"));
@@ -836,7 +835,7 @@ bool _checkDirectPlacement(const QGIViewPart* viewPart, const std::vector<std::s
}
else if (geoType == "Edge") {
int index = TechDraw::DrawUtil::getIndexFromName(subNames[0]);
TechDraw::BaseGeom* geo = static_cast<DrawViewPart*>(viewPart->getViewObject())->getGeomByIndex(index);
TechDraw::BaseGeomPtr geo = static_cast<DrawViewPart *>(viewPart->getViewObject())->getGeomByIndex(index);
if (geo) {
Base::Vector3d midPoint(Rez::guiX(geo->getMidPoint()));
placement = viewPart->mapToScene(midPoint.x, midPoint.y);

View File

@@ -350,11 +350,11 @@ void execMidpoints(Gui::Command* cmd)
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Add Midpont Vertices"));
const std::vector<TechDraw::BaseGeom*> edges = dvp->getEdgeGeometry();
const TechDraw::BaseGeomPtrVector edges = dvp->getEdgeGeometry();
double scale = dvp->getScale();
for (auto& s: selectedEdges) {
int GeoId(TechDraw::DrawUtil::getIndexFromName(s));
TechDraw::BaseGeom* geom = edges.at(GeoId);
TechDraw::BaseGeomPtr geom = edges.at(GeoId);
Base::Vector3d mid = geom->getMidPoint();
mid = DrawUtil::invertY(mid);
dvp->addCosmeticVertex(mid / scale);
@@ -378,11 +378,11 @@ void execQuadrants(Gui::Command* cmd)
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Add Quadrant Vertices"));
const std::vector<TechDraw::BaseGeom*> edges = dvp->getEdgeGeometry();
const TechDraw::BaseGeomPtrVector edges = dvp->getEdgeGeometry();
double scale = dvp->getScale();
for (auto& s: selectedEdges) {
int GeoId(TechDraw::DrawUtil::getIndexFromName(s));
TechDraw::BaseGeom* geom = edges.at(GeoId);
TechDraw::BaseGeomPtr geom = edges.at(GeoId);
std::vector<Base::Vector3d> quads = geom->getQuads();
for (auto& q: quads) {
Base::Vector3d iq = DrawUtil::invertY(q);
@@ -1198,7 +1198,7 @@ void CmdTechDrawCosmeticEraser::activated(int iMsg)
int idx = TechDraw::DrawUtil::getIndexFromName(s);
std::string geomType = TechDraw::DrawUtil::getGeomTypeFromName(s);
if (geomType == "Edge") {
TechDraw::BaseGeom* bg = objFeat->getGeomByIndex(idx);
TechDraw::BaseGeomPtr bg = objFeat->getGeomByIndex(idx);
if ((bg != nullptr) &&
(bg->cosmetic) ) {
int source = bg->source();

View File

@@ -1493,14 +1493,14 @@ int _isValidSingleEdge(Gui::Command* cmd) {
if (SubNames.size() == 1) { //only 1 subshape selected
if (TechDraw::DrawUtil::getGeomTypeFromName(SubNames[0]) == "Edge") { //the Name starts with "Edge"
int GeoId( TechDraw::DrawUtil::getIndexFromName(SubNames[0]) );
TechDraw::BaseGeom* geom = objFeat->getGeomByIndex(GeoId);
TechDraw::BaseGeomPtr geom = objFeat->getGeomByIndex(GeoId);
if (!geom) {
Base::Console().Error("Logic Error: no geometry for GeoId: %d\n",GeoId);
return isInvalid;
}
if(geom->geomType == TechDraw::GENERIC) {
TechDraw::Generic* gen1 = static_cast<TechDraw::Generic *>(geom);
TechDraw::GenericPtr gen1 = std::static_pointer_cast<TechDraw::Generic>(geom);
if(gen1->points.size() > 2) { //the edge is a polyline
return isInvalid;
}
@@ -1519,7 +1519,7 @@ int _isValidSingleEdge(Gui::Command* cmd) {
geom->geomType == TechDraw::ARCOFELLIPSE) {
edgeType = isEllipse;
} else if (geom->geomType == TechDraw::BSPLINE) {
TechDraw::BSpline* spline = static_cast<TechDraw::BSpline*>(geom);
TechDraw::BSplinePtr spline = static_pointer_cast<TechDraw::BSpline> (geom);
if (spline->isCircle()) {
edgeType = isBSplineCircle;
} else {
@@ -1569,8 +1569,8 @@ int _isValidEdgeToEdge(Gui::Command* cmd) {
TechDraw::DrawUtil::getGeomTypeFromName(SubNames[1]) == "Edge") {
int GeoId0( TechDraw::DrawUtil::getIndexFromName(SubNames[0]) );
int GeoId1( TechDraw::DrawUtil::getIndexFromName(SubNames[1]) );
TechDraw::BaseGeom* geom0 = objFeat0->getGeomByIndex(GeoId0);
TechDraw::BaseGeom* geom1 = objFeat0->getGeomByIndex(GeoId1);
TechDraw::BaseGeomPtr geom0 = objFeat0->getGeomByIndex(GeoId0);
TechDraw::BaseGeomPtr geom1 = objFeat0->getGeomByIndex(GeoId1);
if ((!geom0) || (!geom1)) { // missing gometry
Base::Console().Error("Logic Error: no geometry for GeoId: %d or GeoId: %d\n",GeoId0,GeoId1);
@@ -1579,8 +1579,8 @@ int _isValidEdgeToEdge(Gui::Command* cmd) {
if(geom0->geomType == TechDraw::GENERIC &&
geom1->geomType == TechDraw::GENERIC) {
TechDraw::Generic *gen0 = static_cast<TechDraw::Generic *>(geom0);
TechDraw::Generic *gen1 = static_cast<TechDraw::Generic *>(geom1);
TechDraw::GenericPtr gen0 = std::static_pointer_cast<TechDraw::Generic> (geom0);
TechDraw::GenericPtr gen1 = std::static_pointer_cast<TechDraw::Generic> (geom1);
if(gen0->points.size() > 2 ||
gen1->points.size() > 2) { //the edge is a polyline
return isInvalid; //not supported yet
@@ -1609,7 +1609,7 @@ bool _isValidVertexToEdge(Gui::Command* cmd) {
const std::vector<std::string> SubNames = selection[0].getSubNames();
if(SubNames.size() == 2) { //there are 2
int eId,vId;
TechDraw::BaseGeom* e;
TechDraw::BaseGeomPtr e;
TechDraw::VertexPtr v;
if (TechDraw::DrawUtil::getGeomTypeFromName(SubNames[0]) == "Edge" &&
TechDraw::DrawUtil::getGeomTypeFromName(SubNames[1]) == "Vertex") {

View File

@@ -1187,7 +1187,6 @@ void CmdTechDrawExtensionLinePerpendicular::activated(int iMsg)
{
Q_UNUSED(iMsg);
execLineParallelPerpendicular(this,false);
///Base::Console().Message("Create perpendiculararallel line started\n");
}
bool CmdTechDrawExtensionLinePerpendicular::isActive(void)
@@ -1856,14 +1855,15 @@ void _createThreadCircle(std::string Name, TechDraw::DrawViewPart* objFeat, floa
// create the 3/4 arc symbolizing a thread from top seen
double scale = objFeat->getScale();
int GeoId = TechDraw::DrawUtil::getIndexFromName(Name);
TechDraw::BaseGeom* geom = objFeat->getGeomByIndex(GeoId);
TechDraw::BaseGeomPtr geom = objFeat->getGeomByIndex(GeoId);
std::string GeoType = TechDraw::DrawUtil::getGeomTypeFromName(Name);
if (GeoType == "Edge"){
if (geom->geomType == TechDraw::CIRCLE){
TechDraw::Circle* cgen = static_cast<TechDraw::Circle *>(geom);
TechDraw::CirclePtr cgen = std::static_pointer_cast<TechDraw::Circle> (geom);
Base::Vector3d center = cgen->center;
float radius = cgen->radius;
TechDraw::BaseGeom* threadArc = new TechDraw::AOC(center/scale, radius*factor/scale, 255.0, 165.0);
TechDraw::BaseGeomPtr threadArc =
std::make_shared<TechDraw::AOC> (center/scale, radius*factor/scale, 255.0, 165.0);
std::string arcTag = objFeat->addCosmeticEdge(threadArc);
TechDraw::CosmeticEdge* arc = objFeat->getCosmeticEdge(arcTag);
_setLineAttributes(arc);
@@ -1879,11 +1879,11 @@ void _createThreadLines(std::vector<std::string> SubNames, TechDraw::DrawViewPar
if ((GeoType0 == "Edge") && (GeoType1 == "Edge")) {
int GeoId0 = TechDraw::DrawUtil::getIndexFromName(SubNames[0]);
int GeoId1 = TechDraw::DrawUtil::getIndexFromName(SubNames[1]);
TechDraw::BaseGeom* geom0 = objFeat->getGeomByIndex(GeoId0);
TechDraw::BaseGeom* geom1 = objFeat->getGeomByIndex(GeoId1);
TechDraw::BaseGeomPtr geom0 = objFeat->getGeomByIndex(GeoId0);
TechDraw::BaseGeomPtr geom1 = objFeat->getGeomByIndex(GeoId1);
if ((geom0->geomType == TechDraw::GENERIC) && (geom1->geomType == TechDraw::GENERIC)) {
TechDraw::Generic* line0 = static_cast<TechDraw::Generic *>(geom0);
TechDraw::Generic* line1 = static_cast<TechDraw::Generic *>(geom1);
TechDraw::GenericPtr line0 = std::static_pointer_cast<TechDraw::Generic> (geom0);
TechDraw::GenericPtr line1 = std::static_pointer_cast<TechDraw::Generic> (geom1);
Base::Vector3d start0 = line0->points.at(0);
Base::Vector3d end0 = line0->points.at(1);
Base::Vector3d start1 = line1->points.at(0);

View File

@@ -97,9 +97,9 @@ void QGIDrawingTemplate::draw()
// Clear the previous geometry stored
// Get a list of geometry and iterate
const std::vector<TechDraw::BaseGeom *> &geoms = tmplte->getGeometry();
const TechDraw::BaseGeomPtrVector &geoms = tmplte->getGeometry();
std::vector<TechDraw::BaseGeom *>::const_iterator it = geoms.begin();
TechDraw::BaseGeomPtrVector::const_iterator it = geoms.begin();
QPainterPath path;
@@ -109,7 +109,7 @@ void QGIDrawingTemplate::draw()
switch((*it)->geomType) {
case TechDraw::GENERIC: {
TechDraw::Generic *geom = static_cast<TechDraw::Generic *>(*it);
TechDraw::GenericPtr geom = std::static_pointer_cast<TechDraw::Generic>(*it);
path.moveTo(geom->points[0].x, geom->points[0].y);
std::vector<Base::Vector3d>::const_iterator it = geom->points.begin();

View File

@@ -314,7 +314,7 @@ QGraphicsPathItem* QGIFace::lineFromPoints(Base::Vector3d start, Base::Vector3d
return fillItem;
}
QGraphicsPathItem* QGIFace::geomToLine(TechDraw::BaseGeom* base, LineSet& ls)
QGraphicsPathItem* QGIFace::geomToLine(TechDraw::BaseGeomPtr base, LineSet& ls)
{
QGraphicsPathItem* fillItem = new QGraphicsPathItem(this);
Base::Vector3d start(base->getStartPoint().x,
@@ -331,7 +331,7 @@ QGraphicsPathItem* QGIFace::geomToLine(TechDraw::BaseGeom* base, LineSet& ls)
//! make a fragment (length = remain) of a dashed line, with pattern starting at +offset
QGraphicsPathItem* QGIFace::geomToStubbyLine(TechDraw::BaseGeom* base, double remain, LineSet& ls)
QGraphicsPathItem* QGIFace::geomToStubbyLine(TechDraw::BaseGeomPtr base, double remain, LineSet& ls)
{
QGraphicsPathItem* fillItem = new QGraphicsPathItem(this);
Base::Vector3d start(base->getStartPoint().x,

View File

@@ -106,9 +106,9 @@ public:
void clearFillItems(void);
void lineSetToFillItems(TechDraw::LineSet& ls);
QGraphicsPathItem* geomToLine(TechDraw::BaseGeom* base, TechDraw::LineSet& ls);
// QGraphicsPathItem* geomToOffsetLine(TechDraw::BaseGeom* base, double offset, const TechDraw::LineSet& ls);
QGraphicsPathItem* geomToStubbyLine(TechDraw::BaseGeom* base, double offset, TechDraw::LineSet& ls);
QGraphicsPathItem* geomToLine(TechDraw::BaseGeomPtr base, TechDraw::LineSet& ls);
// QGraphicsPathItem* geomToOffsetLine(TechDraw::BaseGeomPtr base, double offset, const TechDraw::LineSet& ls);
QGraphicsPathItem* geomToStubbyLine(TechDraw::BaseGeomPtr base, double offset, TechDraw::LineSet& ls);
QGraphicsPathItem* lineFromPoints(Base::Vector3d start, Base::Vector3d end, TechDraw::DashSpec ds);
//bitmap texture fill parms method
@@ -123,7 +123,7 @@ protected:
std::vector<double> offsetDash(const std::vector<double> dv, const double offset);
QPainterPath dashedPPath(const std::vector<double> dv, const Base::Vector3d start, const Base::Vector3d end);
double dashRemain(const std::vector<double> dv, const double offset);
double calcOffset(TechDraw::BaseGeom* g,TechDraw::LineSet ls);
double calcOffset(TechDraw::BaseGeomPtr g,TechDraw::LineSet ls);
int projIndex; //index of face in Projection. -1 for SectionFace.
QGCustomRect *m_rect;

View File

@@ -142,14 +142,14 @@ void QGIViewPart::setViewPartFeature(TechDraw::DrawViewPart *obj)
setViewFeature(static_cast<TechDraw::DrawView *>(obj));
}
QPainterPath QGIViewPart::drawPainterPath(TechDraw::BaseGeom *baseGeom) const
QPainterPath QGIViewPart::drawPainterPath(TechDraw::BaseGeomPtr baseGeom) const
{
double rot = getViewObject()->Rotation.getValue();
return geomToPainterPath(baseGeom,rot);
}
QPainterPath QGIViewPart::geomToPainterPath(TechDraw::BaseGeom *baseGeom, double rot)
QPainterPath QGIViewPart::geomToPainterPath(BaseGeomPtr baseGeom, double rot)
{
Q_UNUSED(rot);
QPainterPath path;
@@ -160,7 +160,7 @@ QPainterPath QGIViewPart::geomToPainterPath(TechDraw::BaseGeom *baseGeom, double
switch(baseGeom->geomType) {
case CIRCLE: {
TechDraw::Circle *geom = static_cast<TechDraw::Circle *>(baseGeom);
TechDraw::CirclePtr geom = std::static_pointer_cast<TechDraw::Circle>(baseGeom);
double x = geom->center.x - geom->radius;
double y = geom->center.y - geom->radius;
@@ -172,7 +172,7 @@ QPainterPath QGIViewPart::geomToPainterPath(TechDraw::BaseGeom *baseGeom, double
}
break;
case ARCOFCIRCLE: {
TechDraw::AOC *geom = static_cast<TechDraw::AOC *>(baseGeom);
TechDraw::AOCPtr geom = std::static_pointer_cast<TechDraw::AOC> (baseGeom);
if (baseGeom->reversed) {
path.moveTo(Rez::guiX(geom->endPnt.x),
Rez::guiX(geom->endPnt.y));
@@ -203,7 +203,7 @@ QPainterPath QGIViewPart::geomToPainterPath(TechDraw::BaseGeom *baseGeom, double
}
break;
case TechDraw::ELLIPSE: {
TechDraw::Ellipse *geom = static_cast<TechDraw::Ellipse *>(baseGeom);
TechDraw::AOEPtr geom = std::static_pointer_cast<TechDraw::AOE> (baseGeom);
// Calculate start and end points as ellipse with theta = 0 and pi
double startX = geom->center.x + geom->major * cos(geom->angle),
@@ -235,7 +235,7 @@ QPainterPath QGIViewPart::geomToPainterPath(TechDraw::BaseGeom *baseGeom, double
}
break;
case TechDraw::ARCOFELLIPSE: {
TechDraw::AOE *geom = static_cast<TechDraw::AOE *>(baseGeom);
TechDraw::AOEPtr geom = std::static_pointer_cast<TechDraw::AOE> (baseGeom);
if (baseGeom->reversed) {
path.moveTo(Rez::guiX(geom->endPnt.x),
Rez::guiX(geom->endPnt.y));
@@ -266,7 +266,7 @@ QPainterPath QGIViewPart::geomToPainterPath(TechDraw::BaseGeom *baseGeom, double
}
break;
case TechDraw::BEZIER: {
TechDraw::BezierSegment *geom = static_cast<TechDraw::BezierSegment *>(baseGeom);
TechDraw::BezierSegmentPtr geom = std::static_pointer_cast<TechDraw::BezierSegment>(baseGeom);
if (baseGeom->reversed) {
if (!geom->pnts.empty()) {
Base::Vector3d rStart = geom->pnts.back();
@@ -317,7 +317,7 @@ QPainterPath QGIViewPart::geomToPainterPath(TechDraw::BaseGeom *baseGeom, double
}
break;
case TechDraw::BSPLINE: {
TechDraw::BSpline *geom = static_cast<TechDraw::BSpline *>(baseGeom);
TechDraw::BSplinePtr geom = std::static_pointer_cast<TechDraw::BSpline> (baseGeom);
if (baseGeom->reversed) {
// Move painter to the end of our last segment
std::vector<TechDraw::BezierSegment>::const_reverse_iterator it = geom->segments.rbegin();
@@ -372,7 +372,7 @@ QPainterPath QGIViewPart::geomToPainterPath(TechDraw::BaseGeom *baseGeom, double
}
break;
case TechDraw::GENERIC: {
TechDraw::Generic *geom = static_cast<TechDraw::Generic *>(baseGeom);
TechDraw::GenericPtr geom = std::static_pointer_cast<TechDraw::Generic> (baseGeom);
if (baseGeom->reversed) {
if (!geom->points.empty()) {
Base::Vector3d rStart = geom->points.back();
@@ -536,8 +536,8 @@ void QGIViewPart::drawViewPart()
// Draw Edges
QColor edgeColor = PreferencesGui::normalQColor();
const std::vector<TechDraw::BaseGeom *> &geoms = viewPart->getEdgeGeometry();
std::vector<TechDraw::BaseGeom *>::const_iterator itGeom = geoms.begin();
const TechDraw::BaseGeomPtrVector &geoms = viewPart->getEdgeGeometry();
TechDraw::BaseGeomPtrVector::const_iterator itGeom = geoms.begin();
QGIEdge* item;
for(int i = 0 ; itGeom != geoms.end(); itGeom++, i++) {
bool showEdge = false;
@@ -717,17 +717,17 @@ QGIFace* QGIViewPart::drawFace(TechDraw::FacePtr f, int idx)
std::vector<TechDraw::Wire *> fWires = f->wires;
QPainterPath facePath;
for(std::vector<TechDraw::Wire *>::iterator wire = fWires.begin(); wire != fWires.end(); ++wire) {
std::vector<TechDraw::BaseGeom*> geoms = (*wire)->geoms;
TechDraw::BaseGeomPtrVector geoms = (*wire)->geoms;
if (geoms.empty())
continue;
TechDraw::BaseGeom* firstGeom = geoms.front();
TechDraw::BaseGeomPtr firstGeom = geoms.front();
QPainterPath wirePath;
//QPointF startPoint(firstGeom->getStartPoint().x, firstGeom->getStartPoint().y);
//wirePath.moveTo(startPoint);
QPainterPath firstSeg = drawPainterPath(firstGeom);
wirePath.connectPath(firstSeg);
for(std::vector<TechDraw::BaseGeom *>::iterator edge = ((*wire)->geoms.begin()) + 1; edge != (*wire)->geoms.end(); ++edge) {
for(TechDraw::BaseGeomPtrVector::iterator edge = ((*wire)->geoms.begin()) + 1; edge != (*wire)->geoms.end(); ++edge) {
QPainterPath edgePath = drawPainterPath(*edge);
//handle section faces differently
if (idx == -1) {

View File

@@ -74,7 +74,7 @@ public:
virtual void rotateView(void) override;
static QPainterPath geomToPainterPath(TechDraw::BaseGeom *baseGeom, double rotation = 0.0);
static QPainterPath geomToPainterPath(TechDraw::BaseGeomPtr baseGeom, double rotation = 0.0);
/// Helper for pathArc()
/*!
* x_axis_rotation is in radian
@@ -94,7 +94,7 @@ public:
bool getExporting(void) { return m_isExporting; }
protected:
QPainterPath drawPainterPath(TechDraw::BaseGeom *baseGeom) const;
QPainterPath drawPainterPath(TechDraw::BaseGeomPtr baseGeom) const;
void drawViewPart();
QGIFace* drawFace(TechDraw::FacePtr f, int idx);

View File

@@ -91,8 +91,8 @@ TaskCenterLine::TaskCenterLine(TechDraw::DrawViewPart* partFeat,
ui->setupUi(this);
m_geomIndex = DrawUtil::getIndexFromName(m_edgeName);
const std::vector<TechDraw::BaseGeom*> &geoms = partFeat->getEdgeGeometry();
BaseGeom* bg = geoms.at(m_geomIndex);
const TechDraw::BaseGeomPtrVector &geoms = partFeat->getEdgeGeometry();
BaseGeomPtr bg = geoms.at(m_geomIndex);
std::string tag = bg->getCosmeticTag();
m_cl = partFeat->getCenterLine(tag);
if (m_cl == nullptr) { //checked by CommandAnnotate. Should never happen.

View File

@@ -253,9 +253,9 @@ void TaskCosmeticLine::updateCosmeticLine(void)
gp_Pnt gp1(p0.x, p0.y, p0.z);
gp_Pnt gp2(p1.x, p1.y, p1.z);
TopoDS_Edge e = BRepBuilderAPI_MakeEdge(gp1, gp2);
auto oldGeom = m_ce->m_geometry;
// auto oldGeom = m_ce->m_geometry;
m_ce->m_geometry = TechDraw::BaseGeom::baseFactory(e);
delete oldGeom;
// delete oldGeom;
// Gui::Command::updateActive();
// Gui::Command::commitCommand();

View File

@@ -115,7 +115,7 @@ void TaskLineDecor::getDefaults(void)
//set defaults to format of 1st edge
if (!m_edges.empty()) {
int num = DrawUtil::getIndexFromName(m_edges.front());
BaseGeom* bg = m_partFeat->getGeomByIndex(num);
BaseGeomPtr bg = m_partFeat->getGeomByIndex(num);
if (bg != nullptr) {
if (bg->cosmetic) {
if (bg->source() == 1) {
@@ -187,7 +187,7 @@ void TaskLineDecor::applyDecorations(void)
// Base::Console().Message("TLD::applyDecorations()\n");
for (auto& e: m_edges) {
int num = DrawUtil::getIndexFromName(e);
BaseGeom* bg = m_partFeat->getGeomByIndex(num);
BaseGeomPtr bg = m_partFeat->getGeomByIndex(num);
if (bg != nullptr) {
if (bg->cosmetic) {
if (bg->source() == 1) {