[kgraphviewer-devel] [KGraphViewer/libkgraphviz] b589e46: Fix possible crashes on destruction

Kevin Funk krf at electrostorm.net
Fri Jan 7 14:31:49 CET 2011


commit b589e4641b45efe56e2591fb38cfcebbaff650cb
branch libkgraphviz
Author: Kevin Funk <krf at electrostorm.net>
Date:   Fri Jan 7 14:27:42 2011 +0100

    Fix possible crashes on destruction
    
    * Set parent of objects where possible and let Qt handle the deletion
    * Prevent double-free exceptions in other places

diff --git a/src/kgraphviz/canvaselement.cpp b/src/kgraphviz/canvaselement.cpp
index d86952c..04f33c5 100644
--- a/src/kgraphviz/canvaselement.cpp
+++ b/src/kgraphviz/canvaselement.cpp
@@ -102,6 +102,8 @@ CanvasElement::CanvasElement(DotGraphView* v,
 
 CanvasElement::~CanvasElement()
 {
+  kDebug() << element()->label();
+  
   delete d_ptr;
 }
 
diff --git a/src/kgraphviz/canvasnode.cpp b/src/kgraphviz/canvasnode.cpp
index 66fe6e4..8d86825 100644
--- a/src/kgraphviz/canvasnode.cpp
+++ b/src/kgraphviz/canvasnode.cpp
@@ -68,6 +68,10 @@ CanvasNode::CanvasNode(DotGraphView* v,
   setToolTip(tipStr);
 }
 
+CanvasNode::~CanvasNode()
+{
+}
+
 // CanvasHtmlNode::CanvasHtmlNode(
 //                                           DotGraphView* v, 
 //                                           GraphNode* n,
diff --git a/src/kgraphviz/canvasnode.h b/src/kgraphviz/canvasnode.h
index 0b45f02..cf92e72 100644
--- a/src/kgraphviz/canvasnode.h
+++ b/src/kgraphviz/canvasnode.h
@@ -46,7 +46,7 @@ public:
             QGraphicsScene* c,
             QGraphicsItem* parent = 0);
   
-  virtual ~CanvasNode() {}
+  virtual ~CanvasNode();
   
 };
 
diff --git a/src/kgraphviz/dotgraphview.cpp b/src/kgraphviz/dotgraphview.cpp
index f48f484..cdefdf9 100644
--- a/src/kgraphviz/dotgraphview.cpp
+++ b/src/kgraphviz/dotgraphview.cpp
@@ -112,22 +112,12 @@ DotGraphViewPrivate::DotGraphViewPrivate(KActionCollection* actions, DotGraphVie
 
 DotGraphViewPrivate::~DotGraphViewPrivate()
 {
-  Q_Q(DotGraphView);
-
-  delete m_birdEyeView;
-  m_birdEyeView = 0;
+  kDebug() << "enter";
 
-  if (m_popup != 0) {
-    delete m_popup;
-  }
+  delete m_graph;
+  m_graph = 0;
 
-  if (m_canvas) {
-    q->setScene(0);
-    delete m_canvas;
-  }
-  if (m_graph != 0) {
-    delete m_graph;
-  }
+  kDebug() << "leave";
 }
 
 void DotGraphViewPrivate::updateSizes(QSizeF s)
@@ -332,7 +322,7 @@ void DotGraphViewPrivate::setupPopup()
     return;
   }
   kDebug() << "DotGraphView::setupPopup";
-  m_popup = new QMenu();
+  m_popup = new QMenu(q);
   
   m_layoutAlgoSelectAction = new KSelectAction(i18n("Select Layout Algo"),q);
   actionCollection()->addAction("view_layout_algo",m_layoutAlgoSelectAction);
@@ -555,6 +545,8 @@ DotGraphView::DotGraphView(KActionCollection* actions, QWidget* parent) :
 
 DotGraphView::~DotGraphView()
 {
+  kDebug();
+
   delete d_ptr;
 }
 
@@ -611,7 +603,7 @@ void DotGraphViewPrivate::setupCanvas()
   m_xMargin = 50;
   m_yMargin = 50;
 
-  QGraphicsScene* newCanvas = new QGraphicsScene();
+  QGraphicsScene* newCanvas = new QGraphicsScene(q);
   QGraphicsSimpleTextItem* item = newCanvas->addSimpleText(i18n("no graph loaded"));
 //   kDebug() << "Created canvas " << newCanvas;
   
diff --git a/src/kgraphviz/graphedge.cpp b/src/kgraphviz/graphedge.cpp
index e4332d5..bf850a8 100644
--- a/src/kgraphviz/graphedge.cpp
+++ b/src/kgraphviz/graphedge.cpp
@@ -52,7 +52,7 @@ GraphEdge::GraphEdge() :
 
 GraphEdge::~GraphEdge()
 {
-//   kDebug() ;
+  kDebug();
 }
 
 GraphEdge::GraphEdge(const GraphEdge& edge) :
@@ -96,6 +96,8 @@ void GraphEdge::updateWithEdge(const GraphEdge& edge)
   m_arrowheads = edge.arrowheads();
   m_colors = edge.colors();
   m_dir = edge.dir();
+  m_fromNode = edge.m_fromNode;
+  m_toNode = edge.m_toNode;
   GraphElement::updateWithElement(edge);
   if (canvasElement())
   {
diff --git a/src/kgraphviz/graphelement.cpp b/src/kgraphviz/graphelement.cpp
index 07c65e9..5b056b5 100644
--- a/src/kgraphviz/graphelement.cpp
+++ b/src/kgraphviz/graphelement.cpp
@@ -70,7 +70,7 @@ GraphElement::GraphElement() :
 
 GraphElement::GraphElement(const GraphElement& element) :
   QObject(),
-  d_ptr(element.d_ptr)
+  d_ptr(new GraphElementPrivate(*element.d_ptr))
 {
   kDebug() ;
   updateWithElement(element);
@@ -78,19 +78,21 @@ GraphElement::GraphElement(const GraphElement& element) :
 
 GraphElement::~GraphElement()
 {
+  kDebug() << id();
+  
   delete d_ptr;
 }
 
 CanvasElement* GraphElement::canvasElement() const
 {
   Q_D(const GraphElement);
-  return d->m_canvasElement;
+  return d->m_canvasElement.data();
 }
 
 void GraphElement::setCanvasElement(CanvasElement* canvasElement)
 {
   Q_D(GraphElement);
-  d->m_canvasElement = canvasElement;
+  d->m_canvasElement = QSharedPointer<CanvasElement>(canvasElement);
 }
 
 bool GraphElement::isSelected() const
diff --git a/src/kgraphviz/graphelement_p.h b/src/kgraphviz/graphelement_p.h
index 414ee8d..e9df701 100644
--- a/src/kgraphviz/graphelement_p.h
+++ b/src/kgraphviz/graphelement_p.h
@@ -22,6 +22,10 @@
 
 #include "graphelement.h"
 
+#include "canvaselement.h"
+
+#include <QSharedPointer>
+
 namespace KGraphViz
 {
 
@@ -37,7 +41,7 @@ public:
   double m_z;
 
   DotRenderOpVec m_renderOperations;
-  CanvasElement* m_canvasElement;
+  QSharedPointer<KGraphViz::CanvasElement> m_canvasElement;
 };
 
 }
diff --git a/src/kgraphviz/graphio.cpp b/src/kgraphviz/graphio.cpp
index 9223799..b24b38c 100644
--- a/src/kgraphviz/graphio.cpp
+++ b/src/kgraphviz/graphio.cpp
@@ -59,6 +59,7 @@ GraphIOPrivate::GraphIOPrivate(QObject* parent)
 
 GraphIOPrivate::~GraphIOPrivate()
 {
+  kDebug();
 }
 
 void GraphIOPrivate::reset()
diff --git a/src/kgraphviz/graphnode.cpp b/src/kgraphviz/graphnode.cpp
index 377b0cb..840aeaa 100644
--- a/src/kgraphviz/graphnode.cpp
+++ b/src/kgraphviz/graphnode.cpp
@@ -63,9 +63,14 @@ GraphElement(gn)
 GraphNode::GraphNode(node_t* gn) : GraphElement()
 {
   kDebug();
+
   updateWithNode(gn);
 }
 
+GraphNode::~GraphNode()
+{
+}
+
 void GraphNode::updateWithNode(const GraphNode& node)
 {
   kDebug() << id() << node.id();
@@ -84,7 +89,6 @@ void GraphNode::updateWithNode(node_t* node)
   m_attributes["id"] = node->name;
   m_attributes["label"] = ND_label(node)->text;
 
-
   renderOperations().clear();
   if (agget(node, (char*)"_draw_") != NULL)
   {
diff --git a/src/kgraphviz/graphnode.h b/src/kgraphviz/graphnode.h
index 7b6150a..a5aaa93 100644
--- a/src/kgraphviz/graphnode.h
+++ b/src/kgraphviz/graphnode.h
@@ -61,7 +61,7 @@ public:
   GraphNode(const GraphNode& gn);
   GraphNode(Agnode_t* gn);
   
-  virtual ~GraphNode() {}  
+  virtual ~GraphNode();
 
   virtual void updateWithNode(const GraphNode& node);
   virtual void updateWithNode(Agnode_t* node);
diff --git a/src/kgraphviz/graphsubgraph.cpp b/src/kgraphviz/graphsubgraph.cpp
index 0b88cad..8ace9bc 100644
--- a/src/kgraphviz/graphsubgraph.cpp
+++ b/src/kgraphviz/graphsubgraph.cpp
@@ -48,6 +48,10 @@ GraphSubgraph::GraphSubgraph(graph_t* sg) :
   updateWithSubgraph(sg);
 }
 
+GraphSubgraph::~GraphSubgraph()
+{
+}
+
 void GraphSubgraph::updateWithSubgraph(const GraphSubgraph& subgraph)
 {
   kDebug() << id() << subgraph.id();
diff --git a/src/kgraphviz/graphsubgraph.h b/src/kgraphviz/graphsubgraph.h
index f780104..a33c820 100644
--- a/src/kgraphviz/graphsubgraph.h
+++ b/src/kgraphviz/graphsubgraph.h
@@ -51,7 +51,7 @@ public:
   GraphSubgraph();
   explicit GraphSubgraph(Agraph_t* sg);
   
-  virtual ~GraphSubgraph() {}  
+  virtual ~GraphSubgraph();
 
   inline const GraphSubgraphMap& subgraphs() const {return m_subgraphsMap;}
   inline GraphSubgraphMap& subgraphs() {return m_subgraphsMap;}
diff --git a/src/kgraphviz/pannerview.cpp b/src/kgraphviz/pannerview.cpp
index f595089..4a540a5 100644
--- a/src/kgraphviz/pannerview.cpp
+++ b/src/kgraphviz/pannerview.cpp
@@ -73,7 +73,7 @@ PannerViewPrivate::PannerViewPrivate(DotGraphView* parent)
 
 PannerViewPrivate::~PannerViewPrivate()
 {
-
+  kDebug();
 }
 
 PannerView::PannerView(DotGraphView * parent, const char * name)
@@ -97,6 +97,8 @@ PannerView::PannerView(DotGraphView * parent, const char * name)
 
 PannerView::~PannerView()
 {
+  kDebug();
+  
   delete d_ptr;
 }
 


More information about the kgraphviewer-devel mailing list