Results 1 to 5 of 5

Thread: Anyone an idea why this code segfaults?

  1. #1
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Anyone an idea why this code segfaults?

    I have a QDirModel in a QDirTree. I select several files in this view and try to delete them with with following routine:

    Qt Code:
    1. foreach(QModelIndex index,selectionModel()->selectedIndexes()){
    2. if(index.isValid() && index.column()==0){
    3. if(dirModel->isReadOnly()){
    4. return;
    5. }
    6. // Crashes here the second time.
    7. if(dirModel->isDir(index)){
    8. dirModel->rmdir(index);
    9. }else{
    10. dirModel->remove(index);
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. #0 0x00002b2df126227c in QFileInfo::isDir (this=<value optimized out>) at io/qfileinfo.cpp:1059
    2. #1 0x00002b2df0040051 in QDirModel::isDir (this=<value optimized out>, index=@0x7fffbb797ff0)
    3. at itemviews/qdirmodel.cpp:941
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Feb 2006
    Posts
    32
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Anyone an idea why this code segfaults?

    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

  3. The following user says thank you to bmesing for this useful post:

    Kumosan (27th April 2007)

  4. #3
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Anyone an idea why this code segfaults?

    Quote Originally Posted by bmesing View Post
    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.

    Quote Originally Posted by bmesing View Post
    I don't think, that this is reflected in QModelIndex::isValid().
    This was what I nevertheless hoped.

    Quote Originally Posted by bmesing View Post
    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.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Anyone an idea why this code segfaults?

    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).

  6. #5
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Anyone an idea why this code segfaults?

    Quote Originally Posted by wysota View Post
    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.
    Last edited by Kumosan; 28th April 2007 at 06:20. Reason: updated contents

Similar Threads

  1. Pasting code from code tag in emacs
    By Gopala Krishna in forum General Discussion
    Replies: 0
    Last Post: 16th February 2007, 05:47
  2. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15
  3. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  4. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.