Results 1 to 3 of 3

Thread: [solved] Segmentation fault - I don't understand why

  1. #1
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1

    Default [solved] Segmentation fault - I don't understand why

    Hello,

    I have written a short function to build a simple directory tree into a QStringList. Sometimes the function completes successfully but most of the times it does not, and fails with a "Segmentation fault" error on line 32.

    I am modifying the QStringList while iterating through it, but I think it still looks like it should be okay to do this.

    Qt Code:
    1. //
    2. // Sample usage at the bottom
    3. //
    4.  
    5. #include <QDir>
    6. #include <QStringBuilder>
    7.  
    8. void getDirectoryTree(const QDir& directory, QStringList& tree)
    9. {
    10. tree.clear();
    11. tree.push_back("/");
    12.  
    13. for (QStringList::iterator it = tree.begin(); it != tree.end(); ++it)
    14. {
    15. // Real directory or file path
    16. QString currentFilePath(directory.path() % *it);
    17.  
    18. // If this isn't a directory, don't try to look into it.
    19. if (!QFileInfo(currentFilePath).isDir())
    20. {
    21. continue;
    22. }
    23.  
    24. // Real directory
    25. QDir currentDirectory(currentFilePath);
    26.  
    27. // Get list of files in this directory and iterate through them
    28. QStringList subFiles = currentDirectory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
    29. for (QStringList::iterator itSubFile = subFiles.begin(); itSubFile != subFiles.end(); ++itSubFile)
    30. {
    31. // Holds the relative path of this file/directory
    32. QString subFileRelativePath(*it); // <---------- Segmentation fault occurs
    33. subFileRelativePath += *itSubFile;
    34. // Add a directory separator if this is a directory
    35. if (currentDirectory.cd(*itSubFile))
    36. {
    37. currentDirectory.cdUp();
    38. subFileRelativePath += "/";
    39. }
    40.  
    41. // Add it to the directory tree
    42. tree.push_back(subFileRelativePath);
    43. }
    44. }
    45. }
    46.  
    47. //
    48. // Sample usage
    49. //
    50.  
    51. #include <QMessageBox>
    52.  
    53. void MainWindow::on_pushButton_clicked()
    54. {
    55. getDirectoryTree(QDir("D:\\New folder (2)"), tree);
    56. QMessageBox::information(this, "", tree.join("\n"));
    57. }
    To copy to clipboard, switch view to plain text mode 
    If you use Qt Creator, just create a new Qt GUI application, drop a button on the form, add a "clicked()" slot and then it's pretty much just pasting my code into mainwindow.cpp.

    I am feeling pretty frustrated because I don't understand why this problem occurs.
    While I could go a different way (recursive function or use arrays), I feel that this should work one way or another.

    Any help on this and other suggestions for improvement, is much appreciated!

    Thanks in advance!

    Regards,
    Garry
    Last edited by Garry26; 30th September 2010 at 02:03.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Segmentation fault - I don't understand why

    Iterators become invalid when they object they iterate over is modified. While what you're trying to do here may work in some cases, it will quite likely fail in others, as you've discovered.

    Try building a separate QStringList that gets generated through a recursive function call.

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

    Garry26 (30th September 2010)

  4. #3
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1

    Default Re: Segmentation fault - I don't understand why

    Thank you, SixDegrees. I was thinking the same but still sort of assumed it should have worked differently. I'll give it a try with a simple recursive function call and update this post afterwards. I'm pretty sure it will work, I just hoped I could avoid it in a simple and sensible manner.

    Edit: Found QDirIterator. Makes it much easier to walk through a directory tree!
    Last edited by Garry26; 30th September 2010 at 02:00.

Similar Threads

  1. Segmentation Fault
    By freekill in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2010, 16:31
  2. Segmentation fault
    By MarkoSan in forum Qt Programming
    Replies: 23
    Last Post: 19th October 2008, 23:40
  3. segmentation fault
    By mattia in forum Newbie
    Replies: 22
    Last Post: 7th November 2007, 11:37
  4. Segmentation Fault
    By merry in forum General Programming
    Replies: 4
    Last Post: 12th March 2007, 05:08
  5. segmentation fault
    By shamik in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2006, 08:33

Tags for this Thread

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.