Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to detect memory errors in Qt based gui
Forum Updated to NodeBB v4.3 + New Features

How to detect memory errors in Qt based gui

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 5.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #8

    there is crash coming in code QString code . I am want verify after my fix if my fix is without any memory errors

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #9

      I'm quite confident there are no memory leaks in QString code. what seems to be the problem?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #10

        It is not memory leak . I am doing

        if (FILE* fp = fopen(ofilename.toLatin1(), "r")) {
        QTextStream ts(fp, QIODevice::ReadOnly);
        setText(ts.readAll());
        }

        In my case the file size is 1.5G and the tool string because QString can take 1.5 G of memory /. Return type of readAll is QString

        1 Reply Last reply
        0
        • JohanSoloJ Offline
          JohanSoloJ Offline
          JohanSolo
          wrote on last edited by JohanSolo
          #11

          A QString loaded from a 1.5 GB text file needs about 3 GB of memory, as the characters are stored in UTF-16, see the doc. On 32-bits system this almost exceeds the max addressable memory... Depending on how you allocate the QString instance, the consumed memory is far above what is allowed for an application. I therefore think it's normal you have troubles doing this. I would find surprising if you could do this without problems.

          On a side note, I think you were told several times not to try to read such a huge file in one go...

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          Q 1 Reply Last reply
          2
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #12
            1. I'd use QFile instead of fopen()
            2. make sure setText signature is setText(const QString&) and not setText(QString)
            3. can you minimize the memory duplication replacing setText(ts.readAll()); with something like while(!ts.atEnd()) append(ts.readLine());

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            JohanSoloJ 1 Reply Last reply
            2
            • jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #13

              I'm wondering what the use-case is which requires to load 1.5GB text file?
              For such amount of data memory mapped file I/O would be better.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • VRoninV VRonin
                1. I'd use QFile instead of fopen()
                2. make sure setText signature is setText(const QString&) and not setText(QString)
                3. can you minimize the memory duplication replacing setText(ts.readAll()); with something like while(!ts.atEnd()) append(ts.readLine());
                JohanSoloJ Offline
                JohanSoloJ Offline
                JohanSolo
                wrote on last edited by
                #14

                @VRonin said:

                1. make sure setText signature is setText(const QString&) and not setText(QString)

                IIRC the implicit sharing of QString should minimise the impact of having a copy (the 3 GB of memory are not changed).

                `They did not know it was impossible, so they did it.'
                -- Mark Twain

                VRoninV 1 Reply Last reply
                2
                • JohanSoloJ JohanSolo

                  @VRonin said:

                  1. make sure setText signature is setText(const QString&) and not setText(QString)

                  IIRC the implicit sharing of QString should minimise the impact of having a copy (the 3 GB of memory are not changed).

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #15

                  @JohanSolo true but I don't know the content of setText, if you use setText(QString) and then use something as easy as append a new line at the end it triggers a huge copy that const QString& (or QString&) would prevent

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  2
                  • JohanSoloJ JohanSolo

                    A QString loaded from a 1.5 GB text file needs about 3 GB of memory, as the characters are stored in UTF-16, see the doc. On 32-bits system this almost exceeds the max addressable memory... Depending on how you allocate the QString instance, the consumed memory is far above what is allowed for an application. I therefore think it's normal you have troubles doing this. I would find surprising if you could do this without problems.

                    On a side note, I think you were told several times not to try to read such a huge file in one go...

                    Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by Qt Enthusiast
                    #16

                    @JohanSolo
                    On a side note, I think you were told several times not to try to read such a huge file in one go...
                    Hi all
                    I have already made the change in my code after the discussion on forum as follows

                    // BUFFER_SIZE is IMB
                    if (FILE* fp = fopen(ofilename.toLatin1(), "r")) {
                    QTextStream ts(fp, QIODevice::ReadOnly);
                    while (!ts.atEnd()) {
                    QString s = ts.read(BUFFER_SIZE);
                    append(s);
                    }
                    }

                    My problem of QString reading a file of 1.5 BG in one go is gone
                    I am asking for such tool that detects memory errors so as to verify even after my change there are no memory errors . This is just to validate the my changes

                    jsulmJ 1 Reply Last reply
                    0
                    • Q Qt Enthusiast

                      @JohanSolo
                      On a side note, I think you were told several times not to try to read such a huge file in one go...
                      Hi all
                      I have already made the change in my code after the discussion on forum as follows

                      // BUFFER_SIZE is IMB
                      if (FILE* fp = fopen(ofilename.toLatin1(), "r")) {
                      QTextStream ts(fp, QIODevice::ReadOnly);
                      while (!ts.atEnd()) {
                      QString s = ts.read(BUFFER_SIZE);
                      append(s);
                      }
                      }

                      My problem of QString reading a file of 1.5 BG in one go is gone
                      I am asking for such tool that detects memory errors so as to verify even after my change there are no memory errors . This is just to validate the my changes

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #17

                      @Qt-Enthusiast What do you mean by "memory errors"?
                      Do you mean memory leaks? If so then you can use Valgrind as already suggested.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • Q Offline
                        Q Offline
                        Qt Enthusiast
                        wrote on last edited by
                        #18

                        I am try to run valigrind in my tool and will contact you in case of any errors

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          Qt Enthusiast
                          wrote on last edited by
                          #19

                          One more things even after this change , for 1.5 GB file if I move the scrollbar in one go to the end , the GUI hangs for some time . Any reason why this is so . I assume I will see the same behavior with setText

                          1 Reply Last reply
                          0
                          • jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #20

                            I think the Qt editor component is not designed for such huge files, that's probably why it is slow. Who wants to write 1.5GB text files and how long will it take?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved