PDA

View Full Version : Anyone an idea why this code segfaults?



Kumosan
27th April 2007, 20:17
I have a QDirModel in a QDirTree. I select several files in this view and try to delete them with with following routine:


foreach(QModelIndex index,selectionModel()->selectedIndexes()){
if(index.isValid() && index.column()==0){
if(dirModel->isReadOnly()){
return;
}
// Crashes here the second time.
if(dirModel->isDir(index)){
dirModel->rmdir(index);
}else{
dirModel->remove(index);
}
}
}

The first file is deleted normaly, the second time the foreach loop is traversed the code segfaults in 'if(dirModel->isDir(index))'. The core tells me:


#0 0x00002b2df126227c in QFileInfo::isDir (this=<value optimized out>) at io/qfileinfo.cpp:1059
#1 0x00002b2df0040051 in QDirModel::isDir (this=<value optimized out>, index=@0x7fffbb797ff0)
at itemviews/qdirmodel.cpp:941


Maybe I am currently a bit boneheaded, but what is the problem here? this=<value optimized out>? I somewhat expected that the indices might get invalid when I delete a file from the list, but I am testing for that. The pointers are ok, since the first file is successfully deleted. Any ideas?

bmesing
27th April 2007, 21:17
You are changing the model while iterating over it (ok, you are iterating over a selection model, but this ultimately points to your model somehow).
Since QModelIndexes are not guaranteed to be persistent, changing the model might invalidate your remaining indexes.
With invalidating I mean, that using them might result in undefined behaviour, I don't think, that this is reflected in QModelIndex::isValid().

QPersistentModelIndex might be of help here.

Just an idea though.

Regards Ben

Kumosan
27th April 2007, 21:50
You are changing the model while iterating over it (ok, you are iterating over a selection model, but this ultimately points to your model somehow).
Since QModelIndexes are not guaranteed to be persistent, changing the model might invalidate your remaining indexes.


That was what I feared.



I don't think, that this is reflected in QModelIndex::isValid().

This was what I nevertheless hoped.



QPersistentModelIndex might be of help here.


Probably, but not easily since selectedIndexes() returns a QModelIndexList. I am not that eager to create my own QPersistentModelIndexList. I wonder whether there is an easier way to delete a range of files in a QDirModel.

wysota
27th April 2007, 23:11
The general problem is that you try to modify an object which is passed as an argument of a signal and if there are more than one slots connected to that signal, the second (and consecutive) slots may be called for invalid data. That's why you should avoid situations where you delete the signal argument from inside a slot. You can overcome the problem by moving the deletion to after all slots have been fired (for example use a single shot timer with 0 timeout that will perform the actual delete).

Kumosan
28th April 2007, 05:53
The general problem is that you try to modify an object which is passed as an argument of a signal and if there are more than one slots connected to that signal, the second (and consecutive) slots may be called for invalid data. That's why you should avoid situations where you delete the signal argument from inside a slot. You can overcome the problem by moving the deletion to after all slots have been fired (for example use a single shot timer with 0 timeout that will perform the actual delete).

Nope, this is not the problem. No passed object to a slot. In a context menu event a QAction is created, which calls a method 'deleteSelected()'. deleteSelected() has a pointer to the model, fetches the selected indices from that model and starts to delete. No signal/slot or passed parameter involved at all.

I think the QPersistentModel hint was the right idea.

QPersistenModel was definitely the right idea. Problem solved.