Results 1 to 12 of 12

Thread: QTextStream readLine() problem

  1. #1
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextStream readLine() problem

    Hi guys..
    I have some problems reading a file.
    always I run the program, this crash in the same position of file.
    I used gdb to debug and I had this error:

    #0 0x00002aaaabd4c40b in memcpy () from /lib64/libc.so.6
    #1 0x00002aaaaaf9c172 in QString::append (this=0x639260, str=@0x7fff529efe10) at /usr/include/bits/string3.h:52
    #2 0x00002aaaaafe4277 in QTextStreamPrivate::fillReadBuffer (this=0x6391d0, maxBytes=-1) at ../../src/corelib/tools/qstring.h:255
    #3 0x00002aaaaafe4f25 in QTextStreamPrivate::scan (this=0x6391d0, ptr=0x7fff529f3f08, length=0x7fff529f3f1c, maxlen=0, delimiter=QTextStreamPrivate::EndOfLine) at io/qtextstream.cpp:696
    #4 0x00002aaaaafe5a81 in QTextStream::readLine (this=<value optimized out>, maxlen=0) at io/qtextstream.cpp:1584
    #5 0x0000000000401ed2 in main (argc=2, argv=0x7fff529f41b8) at main.cpp:53


    the problem, I think is in.readLine() but I don't know why.

    Could you help me?

    Qt Code:
    1. if(sql_file.open(QIODevice::ReadOnly | QIODevice::Text))
    2. {
    3. QTextStream in(&sql_file);
    4. QString line;
    5. int pos = 0;
    6.  
    7. do {
    8. line = in.readLine(); <------- problem here
    9. qDebug() << "pos: " << pos << " length: " << line.length();
    10.  
    11. ++pos;
    12. //process_line(line);
    13. } while (!line.isNull());
    14.  
    15. sql_file.close();
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    What do you do with pos? Do you use it to seek in the file?
    Can you show more code responsible for reading and navigating in file?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    yes, of course.
    The "pos" var is only to see something and always crash in the same position.

    Qt Code:
    1. QCoreApplication a(argc, argv);
    2.  
    3. QStringList args = QCoreApplication::arguments();
    4.  
    5. QRegExp rxArgPath("^--sql_path=(.*)");
    6.  
    7. for (int i = 1; i < args.size(); ++i)
    8. {
    9. //qDebug() << args.at(i);
    10.  
    11. if(rxArgPath.indexIn(args.at(i), 0) == 0)
    12. {
    13. if(!rxArgPath.cap(1).isEmpty())
    14. {
    15. QFile sql_file(rxArgPath.cap(1));
    16.  
    17. if(sql_file.exists())
    18. {
    19. if(sql_file.open(QIODevice::ReadOnly | QIODevice::Text))
    20. {
    21. qDebug() << "reading file " << rxArgPath.cap(1);
    22.  
    23. QTextStream in(&sql_file);
    24. QString line;
    25. int pos = 0;
    26.  
    27. do {
    28. line = in.readLine();
    29. qDebug() << "pos: " << pos << " length: " << line.length();
    30.  
    31. ++pos;
    32. } while (!line.isNull());
    33.  
    34. sql_file.close();
    35. }
    36. }
    37. else
    38. {
    39. qWarning() << "Er: the file " << rxArgPath.cap(1) << "not exists";
    40. }
    41. }
    42. else
    43. {
    44. qWarning() << "Er: Add the path --sql_path";
    45. }
    46. }
    47. }
    48.  
    49. return a.exec();
    To copy to clipboard, switch view to plain text mode 

    thanks in advance

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    Ahh... this all thing is in your main...
    I think that working with QIODevice needs a running event loop (but I am not sure).
    Put this in to a class, and run this code after the event loop is running, that will be a good idea even if its not the problem.
    Maybe some one else can confirm if this is the problem though.

    EDIT:
    [re-edit] I take back what I said here.
    Last edited by high_flyer; 6th January 2011 at 15:00.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    thz for your reply.

    I moved the code into a class, and I have the same problem

    The file size is 39G, but Im reading line by line, Ithink should have no problem.

    #0 0x00002aaaabfc640b in memcpy () from /lib64/libc.so.6
    #1 0x00002aaaab216172 in QString::append (this=0x6387d0, str=@0x7fff838bbc80) at /usr/include/bits/string3.h:52
    #2 0x00002aaaab25e277 in QTextStreamPrivate::fillReadBuffer (this=0x638740, maxBytes=-1) at ../../src/corelib/tools/qstring.h:255
    #3 0x00002aaaab25ef25 in QTextStreamPrivate::scan (this=0x638740, ptr=0x7fff838bfd78, length=0x7fff838bfd8c, maxlen=0, delimiter=QTextStreamPrivate::EndOfLine) at io/qtextstream.cpp:696
    #4 0x00002aaaab25fa81 in QTextStream::readLine (this=<value optimized out>, maxlen=0) at io/qtextstream.cpp:1584
    #5 0x0000000000402831 in readFile::read (this=0x7fff838bfecf, file_path=@0x7fff838bff60) at read_file.cpp:34
    #6 0x0000000000401e1b in main (argc=2, argv=0x7fff838c0078) at main.cpp:40


    Added after 35 minutes:


    mm I found the problem..

    I modified this line :
    Qt Code:
    1. line = in.readLine();
    To copy to clipboard, switch view to plain text mode 
    to this line:
    Qt Code:
    1. line = in.readLine(100);
    To copy to clipboard, switch view to plain text mode 

    mm, the line is very large, how can I read the big line?

    Thanks
    Last edited by walito; 6th January 2011 at 18:36.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextStream readLine() problem

    The size of the line is not the cause of this problem (unless the line length overflows a size_t). The following code, for example, has no issues other than speed:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. // 40 MB of the letter A
    9. QByteArray b(40 * 1024 * 1024, 'A');
    10. QFile f("test.txt");
    11. if (f.open(QIODevice::WriteOnly)) {
    12. f.write(b);
    13. f.close();
    14. }
    15.  
    16. // Read it line at a time
    17. if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
    18. QTextStream s(&f);
    19. QString line;
    20. line = s.readLine();
    21. qDebug() << line.length() << line.left(10) << line.right(10);
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    Does the problem persist if you completely rebuild the application? That is, is this a binary mismatch between components of the compiled program.

    Edit: I misread your earlier post. If the file is 39 gigabytes and all one line, i.e. no line breaks, and size_t is 32-bits then this may be the issue. This would be an exceptionally unusual text file though. If any one line is longer than the largest RAM allocation available then you will have problems too.
    Last edited by ChrisW67; 7th January 2011 at 00:43. Reason: updated contents

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    Do you have line breaks in the file?
    As far as I know, QTextStream can identify both linux and windows break lines, so no matter what line break encoding you have, it should still get it right...

    What you can also try is to go more low level and use QIODevice::readLine() - see the docs, they have an example too.
    This will allow you to make a small loop, that will read X bytes per read until the whole line is read.
    Depending of what you need to do with the data you read, you can either parse the line parts in each iteration, and reuse your buffer, or, if you need the full string at hand, you can store the read parts in a QStringList for example and parse these...
    The exact way of doing this depends on exactly what you want to do with the read data.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    thz for your reply.

    I was testing and I think the problem is the memory, because the readline() method don't free him buffer and the program crash (memcpy)

    How can I free the memory?

    I'm doing "watch -n1 free -m" and the memory start to down until the program crash.
    I thought the readline() free the memory

    Thanks in advance

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    you didn't answer a very important question:
    do you have line breaks in the file?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    yes yes, exists \n at the end of each row.

    that's weird

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextStream readLine() problem

    Quote Originally Posted by walito View Post
    I was testing and I think the problem is the memory, because the readline() method don't free him buffer and the program crash (memcpy)
    Have you any evidence for this assertion?
    How can I free the memory?
    Allow the QTextStream and QString to go out of scope.
    I'm doing "watch -n1 free -m" and the memory start to down until the program crash.
    Something in your program, that is not in the code we have been shown is allocating memory and not freeing it: perhaps storing data about each line. Eventually, a memory allocation fails and the attempt to copy something into it crashes. That this occurs inside Qt code does not make it a Qt problem.

    If you want to test for actual memory leaks, which are exceptionally unlikely in the relevant Qt code, then use valgrind or something similar. System level tools like free are not useful in finding memory leaks. See this thread for more:
    http://www.qtcentre.org/threads/3742...ee+memory+leak
    I thought the readline() free the memory
    QTextStream and QString both manage their memory.
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. // 3 million lines of 100 characters into temp file
    9. QFile f("/tmp/test.txt");
    10. QByteArray data(99, 'A');
    11. data.append('\n');
    12. qDebug() << "Writing";
    13. if (f.open(QIODevice::WriteOnly)) {
    14. for (int line = 0; line < 3000000; ++line) {
    15. f.write(data);
    16. }
    17. f.close();
    18. }
    19.  
    20. // Read it line at a time
    21. qDebug() << "Reading...";
    22. if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
    23. QTextStream s(&f);
    24. QString line;
    25. while (!f.atEnd())
    26. line = s.readLine();
    27. f.close();
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. $ valgrind --leak-ckeck=yes ./test
    2. ...
    3. Writing
    4. Reading...
    5. ==6051==
    6. ==6051== HEAP SUMMARY:
    7. ==6051== in use at exit: 10,966 bytes in 144 blocks
    8. ==6051== total heap usage: 3,055,495 allocs, 3,055,351 frees, 3,058,183,080 bytes allocated
    9. ==6051==
    10. ...
    11. ==6051== LEAK SUMMARY:
    12. ==6051== definitely lost: 0 bytes in 0 blocks // NO LEAKS
    13. ==6051== indirectly lost: 0 bytes in 0 blocks //
    14. ==6051== possibly lost: 3,136 bytes in 29 blocks
    15. ==6051== still reachable: 7,830 bytes in 115 blocks
    16. ==6051== suppressed: 0 bytes in 0 blocks
    17. ==6051== Reachable blocks (those to which a pointer was found) are not shown.
    18. ==6051== To see them, rerun with: --leak-check=full --show-reachable=yes
    19. ==6051==
    20. ==6051== For counts of detected and suppressed errors, rerun with: -v
    21. ==6051== Use --track-origins=yes to see where uninitialised values come from
    22. ==6051== ERROR SUMMARY: 19 errors from 18 contexts (suppressed: 0 from 0)
    To copy to clipboard, switch view to plain text mode 
    All of the small possible 'leaks' here are coming from libraries outside Qt (glib in my case).

    Have you tried rebuilding the entire application yet?
    Can you produce a small test program and set of data that breaks for you?

  12. #12
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    Thanks for your reply.

    Nice program ( valgrind ) I'll try to test.

    so, I solved the problem using QFile::readline() method, is that's ok? work fine for me.

    I'll try testing using QTextStream::readline(), I need know the problem.

    Thanks all for your help.

    I'll keep you posted

Similar Threads

  1. Problem quitting QThread that uses readLine
    By DiamonDogX in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2010, 16:43
  2. Qtextstream::readline()
    By dawn_to_dusk_ in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2008, 17:58
  3. readLine problem
    By gQt in forum Qt Programming
    Replies: 10
    Last Post: 29th February 2008, 09:35
  4. QTextStream, input and readLine()
    By kramed in forum Newbie
    Replies: 6
    Last Post: 1st September 2007, 23:54
  5. QTextStream problem
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2007, 09:50

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.