Results 1 to 12 of 12

Thread: Why my walker application hangs due to fubction recursion

  1. #1
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why my walker application hangs due to fubction recursion

    I have the app that take the argument as the directory name (finally it should be whole disk C/D/E) and have static function ::walk for it and if the traversed item is directory it is launched the recursive ::walk - but as debugger shows -- if (f.isDir()) {
    Filewalker::walk(f.absoluteFilePath());
    } -- shows the error of sigsegf- segmentation fault-- and I do not know why-due to memory overload, or absent of explicit destructor, or absence of pointer realization of Qlists. The recutrsive error delivers at 4-5 recursion, despite at smaller folders with several folders and level of nesting -- the output is normal. How to absolve this issue or to know exact reason of hanging error?
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QString>
    3. #include <QStringList>
    4. #include <QFile>
    5. #include <QIODevice>
    6. #include <QDir>
    7. #include <QFileInfoList>
    8. #include <QFile>
    9. #include <QList>
    10. #include <QTextStream>
    11. #include <QXmlStreamWriter>
    12. #include <QtDebug>
    13. #include <iostream>
    14. class Filewalker {
    15. public:
    16. long length;
    17. QString name;
    18. QString path;
    19. //static QList <Filewalker> listed;
    20. static QList <QFileInfo> list;
    21. Filewalker(long length1, QString name1, QString path1);
    22. static void walk(QString path0);
    23. static void writexmlfile(QString path1);
    24. static void readxmlfile();
    25. };
    26.  
    27. class Lister {
    28. public:
    29. static QList <Filewalker> listed;
    30. //static QTextStream cout(FILE * fileHandle);
    31. };
    32.  
    33. QList <Filewalker> Lister::listed;
    34.  
    35. Filewalker::Filewalker(long length1, QString name1, QString path1) {
    36. length=length1;
    37. name=name1;
    38. path=path1;
    39. //listed = QList <Filewalker>();
    40. }
    41.  
    42. void Filewalker::walk(QString path0) {
    43. QDir root(path0);
    44. QList <QFileInfo> list = root.entryInfoList(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);
    45. int s=sizeof(Lister::listed);
    46. if (list.isEmpty()) return;
    47. if (list.size()>100) return;
    48. //int s=Lister::listed.size();
    49. foreach (QFileInfo f, list) {
    50. //if (Filewalker::listed.size()>55) break;
    51. if (f.isDir()) {
    52. Filewalker::walk(f.absoluteFilePath());
    53. }
    54. else {
    55. Filewalker odyn (f.size(), f.fileName(), f.path());
    56. Lister::listed.append(odyn);
    57. //odyn.~Filewalker();
    58. QTextStream cout(stdout,QIODevice::WriteOnly);
    59. //cout<<"File:"<<f.size()<<" " << f.fileName()<<" " <<f.path()<<" "<<s<<"\n";
    60. cout<<"File:"<<odyn.length<<" "<< odyn.name<<" "<<odyn.path<<" "<<s<<"\n";
    61. //cout.~QTextStream();
    62. }
    63. }
    64. list.~QList();
    65. root.~QDir();
    66. }
    67. int main(int argc, char *argv[])
    68. {
    69. QCoreApplication a(argc, argv);
    70. Filewalker::walk("C:\\Documents and Settings");
    71. return a.exec();
    72.  
    73. }
    To copy to clipboard, switch view to plain text mode 


    Added after 8 minutes:


    In reality the error message shows such text--"Filewalker.exe-the error is found. The application will be closed. Sorry for inconvenience.
    If the work is not finished the workdata could be lost.
    Pass Microsoft the knowledege about error" (it is my translation of message to english).
    So error is on the part of microsoft - despite dozens of fileinfos is displayed.
    But If i put ::walk(C:\Windows)--the biggest folder- I just see blinking cursor in the first line of console and its idling.
    Last edited by artt; 10th December 2015 at 01:41.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Why my walker application hangs due to fubction recursion

    But If i put ::walk(C:\Windows)--the biggest folder- I just see blinking cursor in the first line of console and its idling.
    The error is *yours*, not Microsoft's. "C:\Windows" doesn't exist. "C:\\Windows" might, though.

    Qt Code:
    1. list.~QList();
    2. root.~QDir();
    To copy to clipboard, switch view to plain text mode 

    Why on earth are you deleting things you create on the stack? This is only one of the many things wrong with your code that could cause a crash.

    You aren't checking for symbolic links, which could lead to infinite recursion, stack overflow, and a crash.

    Why are you creating multiple instances of FileWalker (with different initializations), when all of the FileWalker methods are static?

    You create a FileLister instance on the stack (odyn), then you append it to a Lister's list, and then it goes out of scope when you exit the if() clause. What do you think will happen at that point?

    Instead of just writing more random code and hoping something will work (as it appears you are doing), write down in words, not code, what your program should do, and then design some classes and logic that will accomplish that. What you have now is so confused and full of bugs that you would be better off just throwing it out and starting over.

    And before you write more code, open your C++ book and learn the difference between object instances created on the stack and those created on the heap.
    Last edited by d_stranz; 10th December 2015 at 18:32.

  3. #3
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why my walker application hangs due to fubction recursion

    Of course, I used "C:\\Windows" in code itself.
    Qt Code:
    1. list.~QList();
    2. root.~QDir();
    To copy to clipboard, switch view to plain text mode 
    - Do not return the error but it is really error to create such object on stack-
    Qt Code:
    1. Filewalker odyn (f.size(), f.fileName(), f.path())
    To copy to clipboard, switch view to plain text mode 
    - but debugger return segmentation fault on recursion line or
    Qt Code:
    1. QList <QFileInfo> list = root.entryInfoList(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);
    To copy to clipboard, switch view to plain text mode 
    - in last case I cannot put it with new operator or pointers?? or it possible?
    Just added (...QDir::NoSymLinks) -- the diplay appeared more restricted - just 2 lines of file properties instead about dozen in previous case.
    "Why are you creating multiple instances of FileWalker (with different initializations), when all of the FileWalker methods are static?" - this lines I do not undestand.
    Every filewalker for every file properties? "with different initializations" - what you mean under different initialization?
    Of course I could use all static methods in anotehr class - f.e. in Lister -- but static variables and methods are some global ones- and are initialized before the object creation.
    "You create a FileLister instance on the stack (odyn), then you append it to a Lister's list, and then it goes out of scope when you exit the if() clause." - here maybe I do not understand some c++ fundamentals (sepite I have read most of main C++ and recently Qt books) - "and then it goes out of scope when you exit the if() clause." - did it means that listed is not appended?
    This program is some analogue of Java one - that was designed by me and works very well.
    It first craete the vector(list, here, as Qvector has no append fuction). Then I could write this properties to file (but converted in xml in advance) or render directly from this list of Filewalker, transformed to xml, and displayed in some kind of Textarea(multiline). And this system should be able to search by file name (by Filewalker name fileds or by XML name field ).


    Added after 58 minutes:


    When I change to:
    QList <Filewalker*> Lister::listed;
    ....Filewalker* odyn = new Filewalker (f.size(), f.fileName(), f.path());
    I got the same result (about dozens of lines with results and filewalker.exe console (memory) crash).
    When I add something lower - the destructor
    delete odyn - I got even worse result witn about 5 lines of display.
    ...
    Trying to put listed on heap I got
    QList* <Filewalker*> Lister::listed=new QList <Filewalker*>(); -- expected constructor, destructor, or type conversion before '*' token. So I am doubted that I can initialize static list with new operator.
    Anyway after some changes with new operator - one option brings the SIGSEGF error during debugging
    under the line of:
    QList <QFileInfo> list = root.entryInfoList(QDir::Files|QDir:irs|QDir::NoDotAndDotDot);


    Added after 31 minutes:


    Without creating the Filewalker objects(odyn), just using fileinfo instances "f",
    Qt Code:
    1. cout<<"File:"<<f.size()<<" " << f.fileName()<<" " <<f.path()<<" "<<s<<"\n";
    To copy to clipboard, switch view to plain text mode 
    I got the same results, and debugger show the recursion line. So in this case the issue is in recursion or
    in QFileInfoList list as it probably could not be put at hip as it is using the factory method?
    Anyway if just to transform qfileinfo to xml and add to xml file I need to use recursion.
    In Java <<File[] list = root.listFiles();>> it is similar but works in memory management and the same recursion.

    Without creating the Filewalker objects(odyn), just using fileinfo instances "f",
    Qt Code:
    1. cout<<"File:"<<f.size()<<" " << f.fileName()<<" " <<f.path()<<" "<<s<<"\n";
    To copy to clipboard, switch view to plain text mode 
    I got the same results, and debugger show the recursion line. So in this case the issue is in recursion or
    in QFileInfoList list as it probably could not be put at hip as it is using the factory method?
    Anyway if just to transform qfileinfo to xml and add to xml file I need to use recursion.
    In Java <<File[] list = root.listFiles();>> it is similar but works in memory management and the same recursion.


    Added after 19 minutes:


    Without creating the Filewalker objects(odyn), just using fileinfo instances "f",
    Qt Code:
    1. cout<<"File:"<<f.size()<<" " << f.fileName()<<" " <<f.path()<<" "<<s<<"\n";
    To copy to clipboard, switch view to plain text mode 
    I got the same results, and debugger show the recursion line. So in this case the issue is in recursion or
    in QFileInfoList list as it probably could not be put at hip as it is using the factory method?
    Anyway if just to transform qfileinfo to xml and add to xml file I need to use recursion.
    In Java <<File[] list = root.listFiles();>> it is similar but works in memory management and the same recursion.
    Last edited by artt; 10th December 2015 at 23:20.

  4. #4
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why my walker application hangs due to fubction recursion

    Can I ask a Qt technical support about how to put QFileInfoList on heap? As in my case I got just 15 results in console display(despite due to recursion).

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why my walker application hangs due to fubction recursion

    Can I ask a Qt technical support about how to put QFileInfoList on heap?
    Not sure why you are asking this here, but the very point of having a support contract is that you can ask the support staff.

    If you think it is worthwhile to spend your money on asking how to use the new operator on a type, then that is what you should do.
    However, you might want to consider that any basic C++ tutorial will answer that as well.

    Cheers,
    _

    as Qvector has no append fuction
    Must be an error in the documentation the it claims there is.
    Interestingly the source itself also claims that.

  6. #6
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why my walker application hangs due to fubction recursion

    It should be "fine" mistake in QtAssistant.
    Should the technical support payable?
    You are probably wrong - I can use "new" operator but can it help- but probably should. As debugger in lower (not terminal) console show the heap memory corruption.
    If you mean this approach ("new" by default) from http://stackoverflow.com/questions/6...r-on-the-stack -
    QFileInfoList * dirStuff = new QFileInfoList();
    *dirStuff = dir->entryInfoList(QDir:irs | QDir::Files | QDir::NoDotAndDotDot |
    QDir::System | QDir::Hidden);
    I will check it.
    As my QFileInfoList is on stack it can withstand just 15 items (in case of recursion, despite I do not know do the latter influence it).
    Last edited by artt; 12th December 2015 at 16:21.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why my walker application hangs due to fubction recursion

    It should be "fine" mistake in QtAssistant.
    QVector::append() is also listed in QtAssistant, it shows the same document as the online help

    Should the technical support payable?
    I am not sure what you mean.

    If you mean this approach ("new" by default) from http://stackoverflow.com/questions/6...r-on-the-stack -
    Yes. Really not related to Qt at all, that is what C++ uses for any type.

    Qt Code:
    1. QFileInfoList * dirStuff = new QFileInfoList();
    2. *dirStuff = dir->entryInfoList(QDir:irs | QDir::Files | QDir::NoDotAndDotDot |
    3. QDir::System | QDir::Hidden);
    To copy to clipboard, switch view to plain text mode 
    That should work, if you need that for whatever reason.

    As my QFileInfoList is on stack it can withstand just 15 items
    QFileInfoList, or more generically QList<T>, can easily holds thousands of items even when allocated on the stack.
    Because it allocates its memory on the heap.

    Cheers,
    _

  8. #8
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why my walker application hangs due to fubction recursion

    1. Really, QVector has append() function- as it is the first function in QAssistant-may I overlooked it.
    2. Under "payable" I mean - should I pay for such technical support?
    3. It is very interesting that we can stack initialized object with heap memory--despite it - I initialized the
    QFileInfoList in heap memory (in the code lower) -- but it againt return the 15 results-so just 15 fileinfos processed.
    Qt Code:
    1. QFileInfoList* list=new QFileInfoList();
    2. *list = root.entryInfoList(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);
    3. int s=sizeof(Lister::listed);
    4. if (list->isEmpty()) return;
    5. if (list->size()>100) return;
    6. //int s=Lister::listed.size();
    7. foreach (const QFileInfo &f, *list) {
    8. //if (Filewalker::listed.size()>55) break;
    9. if (f.isDir()) {
    10. Filewalker::walk(f.absoluteFilePath());//SIGSEGV
    11. }
    12. else {
    To copy to clipboard, switch view to plain text mode 
    And in debug mode - it returns the SIGSEGV (segmentation violation) in recursive Filewalker::walk(f.absoluteFilePath());
    In normal mode:I see the message--that app finished unexpectedly (or crashed in another line).//Sorry I could not provide exact message in english as it is not in English.
    So I even could not know how to overcome SIGSEGV in recursive statement???


    Added after 20 minutes:


    *list = root->entryInfoList(QDir::Files|QDir:irs|QDir::NoDotAndDotDot); -- it lead to error -
    base operand of '->' has non-pointer type 'QDir'


    Added after 18 minutes:


    Here are the full description of crash in debugger:
    Qt Code:
    1. Could not load shared library symbols for 13 libraries, e.g. C:\WINDOWS\system32\ntdll.dll.
    2. Use the "info sharedlibrary" command to see the complete listing.
    3. Do you need "set solib-search-path" or "set sysroot"?File:262144 ntuser.dat C:/Documents and Settings/All Users 4
    4. Could not load shared library symbols for C:\WINDOWS\system32\winmm.dll.
    5. Do you need "set solib-search-path" or "set sysroot"?Could not load shared library symbols for C:\WINDOWS\system32\uxtheme.dll.
    6. .......
    7. Do you need "set solib-search-path" or "set sysroot"?Could not load shared library symbols for C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\comctl32.dll.
    8. Do you need "set solib-search-path" or "set sysroot"?Could not load shared library symbols for C:\WINDOWS\system32\comctl32.dll.
    9. ......
    10. Do you need "set solib-search-path" or "set sysroot"?File:32256 Windows Update.lnk C:/Documents and Settings/All Users/Главное меню 4
    11. File:8192 111.lnk C:/Documents and Settings/All Users/Главное меню 4
    12. Could not load shared library symbols for C:\WINDOWS\system32\mlang.dll.
    13. Do you need "set solib-search-path" or "set sysroot"?File:348160 Portable License Utility.lnk C:/Documents and Settings/All Users/Главное меню/Программы/3ds max 7 4
    14. Heap corruption detected at 00D21710
    To copy to clipboard, switch view to plain text mode 
    Last edited by artt; 12th December 2015 at 20:43.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why my walker application hangs due to fubction recursion

    2. Under "payable" I mean - should I pay for such technical support?
    You were considering asking Qt technical support, so I assumed you either had a support contract or were considering getting one.
    You can of course ask trivial C++ questions as part of that package, but why not spent the money when you are actually having a Qt related isssue?

    It is very interesting that we can stack initialized object with heap memory
    The code executed by an object's constructor has the same options as code running inside a function.
    Since function code can allocate heap memory, so can the constructor.

    Most non trivial classes do that in one way or another.

    but it againt return the 15 results-so just 15 fileinfos processed.
    If the directory only has 15 entries, then the entryInfoList() function will return 15 instances of QFileInfo. no matter how you store it.
    Keeping the list on the stack will at least avoid having to deal with deletion, right now your code leaks the list.

    *list = root->entryInfoList(QDir::Files|QDir:irs|QDir::NoDotAnd DotDot); -- it lead to error -
    base operand of '->' has non-pointer type 'QDir'
    As the compiler said, "root" is not a pointer, "->" dereferences a pointer.

    In any case, you might want to have a look at QDirIterator.

    Cheers,
    _

  10. #10
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why my walker application hangs due to fubction recursion

    It happened - so I do not need use QDirIterator, as it also should have issue with the recursion.
    (But 15 results was displayed for 2 or 3 folders that have far more files).
    To resolve issue it was really simple - just
    to put the entrance(initial) directory in the heap memory, and delete it in the end.
    Qt Code:
    1. QDir* root=new QDir(path0);
    2. QFileInfoList* list=new QFileInfoList();
    3. *list = root->entryInfoList(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);
    4. ........
    5. And in the end of function--
    6. delete root;
    7. delete list;
    To copy to clipboard, switch view to plain text mode 
    As the list of fileinfos is the same for release and debug mode -- about of 5 thousands items- I think the issue
    is resolved.
    Despite - in debug mode -
    there are 4-5 .lnk files with aforementioned error messages:
    File:126976 Site Information.lnk C:/Documents and Settings/All Users/Главное меню/Программы/ 10.0/Utilities 4
    Could not load shared library symbols for C:\WINDOWS\system32\shdocvw.dll.
    Do you need "set solib-search-path" or "set sysroot"?Could not load shared library symbols for C:\WINDOWS\system32\crypt32.dll...
    Anyway in this case I just display the qfileinfos. But I need also write it in Qlist of filewalker objects.


    Added after 16 minutes:


    But I cannot initialize QList* <Filewalker*> Lister::listed=new QList <Filewalker*>() to place it on heap (or how do do i correctly?);
    Just on stack - QList <Filewalker*> Lister::listed;
    Last edited by artt; 13th December 2015 at 14:44.

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why my walker application hangs due to fubction recursion

    It happened - so I do not need use QDirIterator, as it also should have issue with the recursion.
    QDirIterator does not have any problem with recursion

    to put the entrance(initial) directory in the heap memory, and delete it in the end.
    Which of course is effectively the same as putting it on the stack, just that you now have to be sure not to return before the end.

    QList* <Filewalker*> Lister::listed=new QList <Filewalker*>()
    Assuming you want a QList<Filewalker*> then use that as the type of the pointer for the variable.
    Qt Code:
    1. QList<Filewalker*>* listed = ....;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  12. #12
    Join Date
    Mar 2015
    Posts
    125
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why my walker application hangs due to fubction recursion

    Yes it works.

Similar Threads

  1. [Qt 5] Dependency Walker and 32/64 bit dll
    By Salads in forum Newbie
    Replies: 1
    Last Post: 13th February 2013, 04:14
  2. QProcess start hangs application
    By hakermania in forum Newbie
    Replies: 3
    Last Post: 10th September 2011, 08:43
  3. Replies: 3
    Last Post: 17th April 2011, 12:05
  4. Application Hangs in QTcpConnection
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2009, 05:21
  5. Qtopia example execution error(application hangs)
    By devendra in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 17th October 2006, 09:11

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.