From 1072d0da9368018b20973e6536c06cacad7e302e Mon Sep 17 00:00:00 2001 From: blacksun Date: Mon, 2 Jan 2017 18:15:18 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20bug=20report=20N=C2=B0=20122,=20and=20fix?= =?UTF-8?q?=20a=20memory=20leak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4829 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- sources/editor/elementscene.cpp | 72 ++++++++++++++++++++------------- sources/editor/elementscene.h | 20 ++++----- 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/sources/editor/elementscene.cpp b/sources/editor/elementscene.cpp index 9187d5f77..4a62b096a 100644 --- a/sources/editor/elementscene.cpp +++ b/sources/editor/elementscene.cpp @@ -44,24 +44,32 @@ ElementScene::ElementScene(QETElementEditor *editor, QObject *parent) : QGraphicsScene(parent), m_elmt_type("simple"), qgi_manager(this), - m_event_interface(nullptr), - element_editor(editor), - decorator_(0) + element_editor(editor) { behavior = Normal; setItemIndexMethod(NoIndex); setGrid(1, 1); initPasteArea(); undo_stack.setClean(); - decorator_lock_ = new QMutex(QMutex::NonRecursive); + m_decorator_lock = new QMutex(QMutex::NonRecursive); connect(&undo_stack, SIGNAL(indexChanged(int)), this, SLOT(managePrimitivesGroups())); connect(this, SIGNAL(selectionChanged()), this, SLOT(managePrimitivesGroups())); } -/// Destructeur -ElementScene::~ElementScene() { - delete decorator_lock_; - if (m_event_interface) delete m_event_interface; +/** + * @brief ElementScene::~ElementScene + */ +ElementScene::~ElementScene() +{ + //Disconnect to avoid crash, see bug report N° 122. + disconnect(&undo_stack, SIGNAL(indexChanged(int)), this, SLOT(managePrimitivesGroups())); + delete m_decorator_lock; + + if (m_event_interface) + delete m_event_interface; + + if (m_decorator) + delete m_decorator; } /** @@ -782,15 +790,19 @@ void ElementScene::getPasteArea(const QRectF &to_paste) { * @brief ElementScene::reset * Remove all QGraphicsItems in the scene and clear the undo stack. */ -void ElementScene::reset() { +void ElementScene::reset() +{ clearSelection(); undoStack().clear(); - foreach (QGraphicsItem *qgi, items()) { + foreach (QGraphicsItem *qgi, items()) + { removeItem(qgi); qgiManager().release(qgi); } - decorator_ = 0; + + delete m_decorator; + m_decorator = nullptr; } /** @@ -1016,35 +1028,39 @@ void ElementScene::centerElementToOrigine() { } /** - Ensure the decorator is adequately shown, hidden or updated so it always - represents the current selection. -*/ -void ElementScene::managePrimitivesGroups() { - // this function is not supposed to be reentrant - if (!decorator_lock_ -> tryLock()) return; + * @brief ElementScene::managePrimitivesGroups + * Ensure the decorator is adequately shown, hidden or updated so it always + * represents the current selection. + */ +void ElementScene::managePrimitivesGroups() +{ + //this function is not supposed to be reentrant + if (!m_decorator_lock->tryLock()) + return; - if (!decorator_) { - decorator_ = new ElementPrimitiveDecorator(); - connect(decorator_, SIGNAL(actionFinished(ElementEditionCommand*)), this, SLOT(stackAction(ElementEditionCommand *))); - addItem(decorator_); - decorator_ -> hide(); + if (!m_decorator) + { + m_decorator = new ElementPrimitiveDecorator(); + connect(m_decorator, SIGNAL(actionFinished(ElementEditionCommand*)), this, SLOT(stackAction(ElementEditionCommand *))); + addItem(m_decorator); + m_decorator -> hide(); } // should we hide the decorator? QList selected_items = zItems(ElementScene::Selected | ElementScene::IncludeTerminals); if (selected_items.size() == 0) - decorator_ -> hide(); + m_decorator -> hide(); else if (selected_items.size() == 1 && selected_items.first()->type() != PartText::Type && selected_items.first()->type() != PartTextField::Type) - decorator_->hide(); + m_decorator->hide(); else { - decorator_ -> setZValue(1000000); - decorator_ -> setPos(0, 0); - decorator_ -> setItems(selected_items); + m_decorator -> setZValue(1000000); + m_decorator -> setPos(0, 0); + m_decorator -> setItems(selected_items); } - decorator_lock_ -> unlock(); + m_decorator_lock -> unlock(); } /** diff --git a/sources/editor/elementscene.h b/sources/editor/elementscene.h index b4a2eaaa8..d8aef5a60 100644 --- a/sources/editor/elementscene.h +++ b/sources/editor/elementscene.h @@ -82,24 +82,24 @@ class ElementScene : public QGraphicsScene { QPointF fsi_pos; QPointF moving_press_pos; - /// Variables related to drawing - ESEventInterface *m_event_interface; + /// Variables related to drawing + ESEventInterface *m_event_interface = nullptr; Behavior behavior; - QETElementEditor *element_editor; + QETElementEditor *element_editor = nullptr; - /// Variables to manage the paste area on the scene + /// Variables to manage the paste area on the scene QGraphicsRectItem *paste_area_; QRectF defined_paste_area_; - /// Variables to handle copy/paste with offset + /// Variables to handle copy/paste with offset QString last_copied_; - /// Decorator item displayed when at least one item is selected - ElementPrimitiveDecorator *decorator_; + /// Decorator item displayed when at least one item is selected + ElementPrimitiveDecorator *m_decorator = nullptr; - ///< Size of the horizontal grid step + ///< Size of the horizontal grid step int x_grid; - ///< Size of the vertical grid step + ///< Size of the vertical grid step int y_grid; // methods @@ -153,7 +153,7 @@ class ElementScene : public QGraphicsScene { void addPrimitive(QGraphicsItem *); void initPasteArea(); static bool zValueLessThan(QGraphicsItem *, QGraphicsItem *); - QMutex *decorator_lock_; + QMutex *m_decorator_lock; void centerElementToOrigine(); public slots: