LyX crashes when handling selected databases in the BibTeX Bibliography dialog.

pdv pdvisschere at edpnet.be
Sun Nov 17 18:50:49 UTC 2019


On macos and with the LyX2.4 master:

In the BibTeX Bibliography dialog: when handling items in the selected 
databases list (moving items up/down or adding items) LyX crashes with 
an EXC_BAD_ACCESS.
(LyX2.3.1 does not show this problem.)

Possible solution:

In GuiSelectionManager.cpp the routines addPB_clicked(), upPB_clicked() 
and downPB_clicked() define a QModelIndex idx, then make changes to the 
model and use idx again. According to the qt5 documentation a 
QPersistentModelIndex should be used:

	 Note: Model indexes should be used immediately and then discarded. You 
should not rely on indexes to remain valid after calling model functions 
that change the structure of the model or delete items. If you need to 
keep a model index over time use a QPersistentModelIndex.
	
In addPB_clicked() the idx is only used in one place and it’s definition 
can be moved. In upPB_clicked() and downPB_clicked() I first made idx of 
type QPersistentModelIndex and this avoids the crashes but the selection 
is lost when moving an item up/down.

I then redefined idx before the 2nd use and removed the 
selectionChanged() signals and this avoids the crashes and keeps the 
selection intact (patch included).

P. De Visschere
-------------- next part --------------
diff --git a/src/frontends/qt/GuiSelectionManager.cpp b/src/frontends/qt/GuiSelectionManager.cpp
index 4a735ed2d6..d77acbadc6 100644
--- a/src/frontends/qt/GuiSelectionManager.cpp
+++ b/src/frontends/qt/GuiSelectionManager.cpp
@@ -285,14 +285,14 @@ void GuiSelectionManager::addPB_clicked()
 		return;
 
 	QModelIndex const idxToAdd = selIdx.first();
-	QModelIndex const idx = selectedLV->currentIndex();
 	int const srows = selectedModel->rowCount();
 
 	QMap<int, QVariant> qm = availableModel->itemData(idxToAdd);
 	insertRowToSelected(srows, qm);
 
 	selectionChanged(); //signal
-
+	
+	QModelIndex const idx = selectedLV->currentIndex();
 	if (idx.isValid())
 		selectedLV->setCurrentIndex(idx);
 
@@ -344,8 +344,7 @@ void GuiSelectionManager::upPB_clicked()
 	selectedModel->removeRow(pos);
 	insertRowToSelected(pos - 1, qms);
 
-	selectionChanged(); //signal
-
+	idx = selIdx.first();
 	selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
 	selectedHasFocus_ = true;
 	updateHook();
@@ -372,8 +371,7 @@ void GuiSelectionManager::downPB_clicked()
 	selectedModel->removeRow(pos);
 	insertRowToSelected(pos + 1, qms);
 
-	selectionChanged(); //signal
-
+	idx = selIdx.first();
 	selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
 	selectedHasFocus_ = true;
 	updateHook();


More information about the lyx-devel mailing list