Index: src/UIElements/Views/Qt4/MoleculeList/QtMoleculeList.cpp
===================================================================
--- src/UIElements/Views/Qt4/MoleculeList/QtMoleculeList.cpp	(revision 1259df9500410b59c3a56d9cf8d54624b53f97da)
+++ src/UIElements/Views/Qt4/MoleculeList/QtMoleculeList.cpp	(revision 9ec4b8fd1c0781bcd20ad52c2755ad52ae89d518)
@@ -61,7 +61,5 @@
 QtMoleculeList::QtMoleculeList() :
   Observer("QtMoleculeList"),
-  changing(false),
   ChangingChildrensVisibility(false),
-  list_accessing(false),
   update_timer(NULL),
   callback_DirtyItems(boost::bind(&QtMoleculeList::informDirtyState, this, _1, _2))
@@ -138,8 +136,7 @@
       {
         const molecule * const mol = World::getInstance().lastChanged<molecule>();
-        while(list_accessing);
-        list_accessing = true;
+        listAccessing_mutex.lock();
         newItems.push_back( mol );
-        list_accessing = false;
+        listAccessing_mutex.unlock();
         break;
       }
@@ -148,8 +145,7 @@
         const molecule * const mol = World::getInstance().lastChanged<molecule>();
 
-        while(list_accessing);
-        list_accessing = true;
+        listAccessing_mutex.lock();
         toBeRemovedItems.push_back( mol ); // remove in any case, as we also got insert
-        list_accessing = false;
+        listAccessing_mutex.unlock();
 
         const QtMoleculeItem *mol_item = MoleculeIdToItem(mol->getId());
@@ -186,8 +182,7 @@
     QStandardItem *_groupitem)
 {
-  while (list_accessing);
-  list_accessing = true;
+  listAccessing_mutex.lock();
   toBeSetOccurrenceItems.insert( _groupitem );
-  list_accessing = false;
+  listAccessing_mutex.unlock();
 }
 
@@ -271,5 +266,5 @@
     update_timer->stop();
 
-  changing = true;
+  refill_mutex.lock();
 
   const std::vector<const molecule*> &molecules =
@@ -294,5 +289,5 @@
     addMolecule(*iter);
 
-  changing = false;
+  refill_mutex.unlock();
 
   // activate timer
@@ -303,6 +298,5 @@
 {
   // get whether any items are dirty
-  while (list_accessing);
-  list_accessing = true;
+  listAccessing_mutex.lock();
   bool dirty = false;
   dirty |= !dirtyItems.empty();
@@ -312,5 +306,5 @@
   dirty |= !toBeSetOccurrenceItems.empty();
   dirty |= !toBeSetVisibilityItems.empty();
-  list_accessing = false;
+  listAccessing_mutex.unlock();
   return dirty;
 }
@@ -333,11 +327,10 @@
   if (_item->index().column() == QtMoleculeItemFactory::VISIBILITY) {
 //    qDebug() << "visibilityItem changed: " << (_item->checkState() ? "checked" : "unchecked");
-    while(list_accessing);
-    list_accessing = true;
+    listAccessing_mutex.lock();
     if ((_item->parent() == NULL) || (_item->parent() == invisibleRootItem()))
       toBeSetVisibilityItems.insert( _item );
     else
       visibilityItems.insert( dynamic_cast<QtMoleculeItem *>(_item) );
-    list_accessing = false;
+    listAccessing_mutex.unlock();
   }
 }
@@ -349,8 +342,12 @@
 
   const bool visible = _item->checkState();
-  const std::string &molecule_formula = _item->getMolecule()->getFormula().toString();
-  ASSERT( FormulaVisibilityCountMap.count(molecule_formula) != 0,
-      "QtMoleculeList::setVisibilityForMoleculeItem() - molecule with formula " +molecule_formula
-      +" is not present in FormulaVisibilityCountMap.");
+  const molecule * const mol = _item->getMolecule();
+  std::string molecule_formula;
+  if (mol != NULL) {
+    molecule_formula = mol->getFormula().toString();
+    ASSERT( FormulaVisibilityCountMap.count(molecule_formula) != 0,
+        "QtMoleculeList::setVisibilityForMoleculeItem() - molecule with formula " +molecule_formula
+        +" is not present in FormulaVisibilityCountMap.");
+  }
 
   // get parent
@@ -360,5 +357,5 @@
   ASSERT( groupItem != NULL,
       "QtMoleculeList::setVisibilityForMoleculeItem() - item with id "
-      +toString(_item->getMolecule()->getId())+" has not parent?");
+      +toString(_item->getMoleculeId())+" has not parent?");
   // check whether we have to set the group item
 
@@ -469,16 +466,20 @@
     addToBeSetOccurrence(groupitem);
   // ..  and re-add where new formula fits
-  const std::string molecule_formula = _molitem->getMolecule()->getFormula().toString();
-  FormulaTreeItemMap_t::iterator iter = formula.find(molecule_formula);
-  if (iter == formula.end()) {
-    // add new group item and formula entry
-     addGroupItem(groupitem, molecule_formula);
-  } else {
-    groupitem = iter->second;
-  }
-  ASSERT( groupitem != NULL,
-      "QtMoleculeList::readdItem() - failed to create a sensible new groupitem");
-  // finally add again
-  groupitem->appendRow(mol_row);
+  const molecule * const mol = _molitem->getMolecule();
+  std::string molecule_formula;
+  if (mol != NULL) {
+    molecule_formula = mol->getFormula().toString();
+    FormulaTreeItemMap_t::iterator iter = formula.find(molecule_formula);
+    if (iter == formula.end()) {
+      // add new group item and formula entry
+       addGroupItem(groupitem, molecule_formula);
+    } else {
+      groupitem = iter->second;
+    }
+    ASSERT( groupitem != NULL,
+        "QtMoleculeList::readdItem() - failed to create a sensible new groupitem");
+    // finally add again
+    groupitem->appendRow(mol_row);
+  }
   // call setOccurrence on the new group later
   addToBeSetOccurrence(groupitem);
@@ -489,8 +490,9 @@
     const QtMoleculeItem::MoveTypes _type)
 {
-  while (list_accessing);
-  list_accessing = true;
-
+  refill_mutex.lock();
+
+  listAccessing_mutex.lock();
   dirtyItems.insert(_item);
+  listAccessing_mutex.unlock();
   if (_type == QtMoleculeItem::NeedsMove) {
     // we have to convert whatever item raised the dirty signal to the first
@@ -500,10 +502,12 @@
     QtMoleculeItem *mol_item =
         dynamic_cast<QtMoleculeItem *>(group_item->child(_item->row(), 0));
+    listAccessing_mutex.lock();
     toBeMovedItems.insert(mol_item);
+    listAccessing_mutex.unlock();
 //    // add group item, too
 //    toBeSetOccurrenceItems.insert(group_item);
   }
 
-  list_accessing = false;
+  refill_mutex.unlock();
 }
 
@@ -511,13 +515,9 @@
 {
   // wait till initial refill has been executed
-  if (changing)
-    return;
-
-  changing = true;
+  refill_mutex.lock();
 
   /// copy lists such that new signals for dirty/.. may come in right away
   // TODO: if we had move semantics ...
-  while (list_accessing);
-  list_accessing = true;
+  listAccessing_mutex.lock();
   list_of_items_t dirtyItems_copy = dirtyItems;
   dirtyItems.clear();
@@ -534,5 +534,5 @@
   std::set<QStandardItem*> toBeSetVisibilityItems_copy = toBeSetVisibilityItems;
   toBeSetVisibilityItems.clear();
-  list_accessing = false;
+  listAccessing_mutex.unlock();
 
   /// first check consistency among the sets:
@@ -667,4 +667,4 @@
 
   // and done
-  changing = false;
-}
+  refill_mutex.unlock();
+}
