From f9d482e1154de57627e5fbc6cb84dd4c95682efc Mon Sep 17 00:00:00 2001
From: Claudio Valerio <claudio@open-sankore.org>
Date: Fri, 16 Nov 2012 09:19:01 +0100
Subject: [PATCH] comment some code to avoid crash on page change. To be
 reworked

---
 src/board/UBBoardController.cpp      | 101 ++++++++++++++-------------
 src/document/UBDocumentContainer.cpp |   4 +-
 src/gui/UBDocumentNavigator.cpp      |   6 +-
 3 files changed, 61 insertions(+), 50 deletions(-)

diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp
index 59ad992b..81117019 100644
--- a/src/board/UBBoardController.cpp
+++ b/src/board/UBBoardController.cpp
@@ -1567,57 +1567,64 @@ void UBBoardController::moveSceneToIndex(int source, int target)
 
 void UBBoardController::ClearUndoStack()
 {
-    QSet<QGraphicsItem*> uniqueItems;
-    // go through all stack command
-    for(int i = 0; i < UBApplication::undoStack->count(); i++)
-    {
-
-        UBAbstractUndoCommand *abstractCmd = (UBAbstractUndoCommand*)UBApplication::undoStack->command(i);
-        if(abstractCmd->getType() != UBAbstractUndoCommand::undotype_GRAPHICITEM)
-            continue;
-
-        UBGraphicsItemUndoCommand *cmd = (UBGraphicsItemUndoCommand*)UBApplication::undoStack->command(i);
-
-        // go through all added and removed objects, for create list of unique objects
-        // grouped items will be deleted by groups, so we don't need do delete that items.
-        QSetIterator<QGraphicsItem*> itAdded(cmd->GetAddedList());
-        while (itAdded.hasNext())
-        {
-            QGraphicsItem* item = itAdded.next();
-            if( !uniqueItems.contains(item) && !(item->parentItem() && UBGraphicsGroupContainerItem::Type == item->parentItem()->type()))
-                uniqueItems.insert(item);
-        }
-
-        QSetIterator<QGraphicsItem*> itRemoved(cmd->GetRemovedList());
-        while (itRemoved.hasNext())
-        {
-            QGraphicsItem* item = itRemoved.next();
-            if( !uniqueItems.contains(item) && !(item->parentItem() && UBGraphicsGroupContainerItem::Type == item->parentItem()->type()))
-                uniqueItems.insert(item);
-        }
-    }
+// The code has been removed because it leads to a strange error and because the final goal has never been
+// reached on tests and sound a little bit strange.
+// Strange error: item->scene() crashes the application because item doesn't implement scene() method. I'm
+// not able to give all the steps to reproduce this error sistematically but is quite frequent (~ twice per utilisation hours)
+// strange goal: if item is on the undocommand, the item->scene() is null and the item is not on the deleted scene item list then
+// then it's deleted.
+
+    //    QSet<QGraphicsItem*> uniqueItems;
+//    // go through all stack command
+//    for(int i = 0; i < UBApplication::undoStack->count(); i++)
+//    {
+
+//        UBAbstractUndoCommand *abstractCmd = (UBAbstractUndoCommand*)UBApplication::undoStack->command(i);
+//        if(abstractCmd->getType() != UBAbstractUndoCommand::undotype_GRAPHICITEM)
+//            continue;
+
+//        UBGraphicsItemUndoCommand *cmd = (UBGraphicsItemUndoCommand*)UBApplication::undoStack->command(i);
+
+//        // go through all added and removed objects, for create list of unique objects
+//        // grouped items will be deleted by groups, so we don't need do delete that items.
+//        QSetIterator<QGraphicsItem*> itAdded(cmd->GetAddedList());
+//        while (itAdded.hasNext())
+//        {
+//            QGraphicsItem* item = itAdded.next();
+//            if( !uniqueItems.contains(item) && !(item->parentItem() && UBGraphicsGroupContainerItem::Type == item->parentItem()->type()))
+//                uniqueItems.insert(item);
+//        }
+
+//        QSetIterator<QGraphicsItem*> itRemoved(cmd->GetRemovedList());
+//        while (itRemoved.hasNext())
+//        {
+//            QGraphicsItem* item = itRemoved.next();
+//            if( !uniqueItems.contains(item) && !(item->parentItem() && UBGraphicsGroupContainerItem::Type == item->parentItem()->type()))
+//                uniqueItems.insert(item);
+//        }
+//    }
+
+//    // go through all unique items, and check, ot on scene, or not.
+//    // if not on scene, than item can be deleted
+
+//    QSetIterator<QGraphicsItem*> itUniq(uniqueItems);
+//    while (itUniq.hasNext())
+//    {
+//        QGraphicsItem* item = itUniq.next();
+//        UBGraphicsScene *scene = NULL;
+//        if (item->scene()) {
+//            scene = dynamic_cast<UBGraphicsScene*>(item->scene());
+//        }
+//        if(!scene)
+//        {
+//           if (!mActiveScene->deleteItem(item))
+//               delete item;
+//        }
+//    }
 
     // clear stack, and command list
     UBApplication::undoStack->clear();
 
-    // go through all unique items, and check, ot on scene, or not.
-    // if not on scene, than item can be deleted
-
-    QSetIterator<QGraphicsItem*> itUniq(uniqueItems);
-    while (itUniq.hasNext())
-    {
-        QGraphicsItem* item = itUniq.next();
-        UBGraphicsScene *scene = NULL;
-        if (item->scene()) {
-            scene = dynamic_cast<UBGraphicsScene*>(item->scene());
-        }
-        if(!scene)
-        {
-           if (!mActiveScene->deleteItem(item))
-               delete item;
-        }
-    }
-
 }
 
 void UBBoardController::adjustDisplayViews()
diff --git a/src/document/UBDocumentContainer.cpp b/src/document/UBDocumentContainer.cpp
index 500cc9fb..0ad8b4b1 100644
--- a/src/document/UBDocumentContainer.cpp
+++ b/src/document/UBDocumentContainer.cpp
@@ -34,8 +34,10 @@ UBDocumentContainer::UBDocumentContainer(QObject * parent)
 
 UBDocumentContainer::~UBDocumentContainer()
 {
-    foreach(const QPixmap* pm, mDocumentThumbs)   
+    foreach(const QPixmap* pm, mDocumentThumbs){
         delete pm;
+        pm = NULL;
+    }
 }
 
 void UBDocumentContainer::setDocument(UBDocumentProxy* document, bool forceReload)
diff --git a/src/gui/UBDocumentNavigator.cpp b/src/gui/UBDocumentNavigator.cpp
index 9ac12486..3afbd4b2 100644
--- a/src/gui/UBDocumentNavigator.cpp
+++ b/src/gui/UBDocumentNavigator.cpp
@@ -92,12 +92,13 @@ void UBDocumentNavigator::generateThumbnails(UBDocumentContainer* source)
     {
         mScene->removeItem(it);
         delete it;
+        it = NULL;
     }
 
     for(int i = 0; i < source->selectedDocument()->pageCount(); i++)
     {
-
-    	const QPixmap* pix = source->pageAt(i);
+        const QPixmap* pix = source->pageAt(i);
+        Q_ASSERT(!pix->isNull());
         int pageIndex = UBDocumentContainer::pageFromSceneIndex(i);
 
         UBSceneThumbnailNavigPixmap* pixmapItem = new UBSceneThumbnailNavigPixmap(*pix, source->selectedDocument(), i);
@@ -154,6 +155,7 @@ void UBDocumentNavigator::updateSpecificThumbnail(int iPage)
         mScene->addItem(newItem);
         mThumbsWithLabels[iPage].setThumbnail(newItem);
         delete oldItem;
+        oldItem = NULL;
     }
 
 }