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. insertPlainText valgrind error.
Forum Updated to NodeBB v4.3 + New Features

insertPlainText valgrind error.

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 2 Posters 302 Views 1 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.
  • T TON 618

    Good to know. I used it 1 year ago and then dropped it.
    I picked it up again only recently so that might be it. Waiting for your report and thanks for the fast answers :)

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote last edited by
    #8

    @TON-618
    Obviously I cannot type in exactly the same situation as you are in. But the following works fine for me, with no valgrind warnings on exit (other than the 6 "External errors" you always get if you don't switch that off in the filter, it starts switched off).

    #include <QApplication>
    #include <QTextEdit>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QTextEdit w;
        for (int i = 0; i < 10; i++)
        {
            QByteArray line(QByteArrayLiteral("Hello world!\n"));
            QString str(QString::fromUtf8(line));
            w.insertPlainText(str);
        }
        w.show();
        return a.exec();
    }
    

    You could try this under your Qt5 to see whether that barfs or it is something else in your real code.

    If you are going to move to Qt6 do that soon and check in valgrind, there is no point worrying about Qt5.

    T 1 Reply Last reply
    0
    • JonBJ JonB

      @TON-618
      Obviously I cannot type in exactly the same situation as you are in. But the following works fine for me, with no valgrind warnings on exit (other than the 6 "External errors" you always get if you don't switch that off in the filter, it starts switched off).

      #include <QApplication>
      #include <QTextEdit>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QTextEdit w;
          for (int i = 0; i < 10; i++)
          {
              QByteArray line(QByteArrayLiteral("Hello world!\n"));
              QString str(QString::fromUtf8(line));
              w.insertPlainText(str);
          }
          w.show();
          return a.exec();
      }
      

      You could try this under your Qt5 to see whether that barfs or it is something else in your real code.

      If you are going to move to Qt6 do that soon and check in valgrind, there is no point worrying about Qt5.

      T Offline
      T Offline
      TON 618
      wrote last edited by
      #9

      @JonB thats what i get if i run the program you provided. Thats probably normal looking at the valgrind error. But yeah, at least i'm not getting the errors i get in my program. I'll surely upgrade to the newest version of qt and see what happens anyway since code might change the output in some ways.

      ==39617== realloc() with size 0
      ==39617==    at 0x484DB80: realloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
      ==39617==    by 0xD94DC1F: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.550.144.03)
      ==39617==    by 0xCDEB986: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
      ==39617==    by 0xCE456D1: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
      ==39617==    by 0xCDEB012: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
      ==39617==    by 0x4005823: call_init (dl-init.c:120)
      ==39617==    by 0x4005823: _dl_init (dl-init.c:121)
      ==39617==    by 0x40015B1: _dl_catch_exception (dl-catch.c:211)
      ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:829)
      ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:792)
      ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
      ==39617==    by 0x400D163: _dl_open (dl-open.c:905)
      ==39617==    by 0x5EDF1A3: dlopen_doit (dlopen.c:56)
      ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
      ==39617==  Address 0x8d26140 is 0 bytes after a block of size 0 alloc'd
      ==39617==    at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
      ==39617==    by 0xD94DC0F: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.550.144.03)
      ==39617==    by 0xCDEB986: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
      ==39617==    by 0xCE456D1: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
      ==39617==    by 0xCDEB012: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
      ==39617==    by 0x4005823: call_init (dl-init.c:120)
      ==39617==    by 0x4005823: _dl_init (dl-init.c:121)
      ==39617==    by 0x40015B1: _dl_catch_exception (dl-catch.c:211)
      ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:829)
      ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:792)
      ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
      ==39617==    by 0x400D163: _dl_open (dl-open.c:905)
      ==39617==    by 0x5EDF1A3: dlopen_doit (dlopen.c:56)
      ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
      ==39617== 
      ==39617== posix_memalign() invalid size value: 0
      ==39617==    at 0x484E2E8: posix_memalign (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
      ==39617==    by 0xD94DC55: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.550.144.03)
      ==39617==    by 0xCDEB986: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
      ==39617==    by 0xCE456D1: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
      ==39617==    by 0xCDEB012: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
      ==39617==    by 0x4005823: call_init (dl-init.c:120)
      ==39617==    by 0x4005823: _dl_init (dl-init.c:121)
      ==39617==    by 0x40015B1: _dl_catch_exception (dl-catch.c:211)
      ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:829)
      ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:792)
      ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
      ==39617==    by 0x400D163: _dl_open (dl-open.c:905)
      ==39617==    by 0x5EDF1A3: dlopen_doit (dlopen.c:56)
      ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
      ==39617== 
      ==39617== 
      ==39617== HEAP SUMMARY:
      ==39617==     in use at exit: 145,595 bytes in 1,080 blocks
      ==39617==   total heap usage: 136,759 allocs, 135,679 frees, 2,985,737,013 bytes allocated
      ==39617== 
      ==39617== LEAK SUMMARY:
      ==39617==    definitely lost: 176 bytes in 4 blocks
      ==39617==    indirectly lost: 0 bytes in 0 blocks
      ==39617==      possibly lost: 368 bytes in 1 blocks
      ==39617==    still reachable: 145,051 bytes in 1,075 blocks
      ==39617==         suppressed: 0 bytes in 0 blocks
      ==39617== Rerun with --leak-check=full to see details of leaked memory
      ==39617== 
      ==39617== For lists of detected and suppressed errors, rerun with: -s
      ==39617== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
      
      JonBJ 1 Reply Last reply
      0
      • T TON 618

        Hey there,
        I've recently started build a text editor using qt and c++.
        The program as of now runs just fine.
        I decided to run valgrind to handle leaks and i'm getting tons of error from
        "insertPlainText" function i believe.
        Function is called at the start after the Mode Object has been created.

        #include "../includes/Mode.h"
        #include "../includes/mainwindow.h"
        #include "ui_mainwindow.h"
        #include <qevent.h>
        
        /*@brief: iterates over given file and copy its content.
         * Content is copied inside iTextEdit.
         * This is needed to edit the file passed as parameter to main. */
        
        void Mode::createFile()
        {
          QFile file(this->file);
          if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                return;
          while (!file.atEnd())
          {
                Q_ASSERT(ui.iTextEdit);
                Q_ASSERT(ui.iTextEdit->document());
                QByteArray line = file.readLine();
                ui.iTextEdit->insertPlainText(QString::fromUtf8(line));
          }
          sel_struct->tcurs.setPosition(QTextCursor::Start);
          ui.iTextEdit->setTextCursor(sel_struct->tcurs);
          file.close();
        }
        
        ==21831== Invalid read of size 16
        ==21831==    at 0xC466325: ???
        ==21831==    by 0xB1B5DF7: ???
        ==21831==  Address 0xb1b5e5e is 126 bytes inside a block of size 140 alloc'd
        ==21831==    at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
        ==21831==    by 0x570D0E9: QArrayData::allocate(unsigned long, unsigned long, unsigned long, QFlags<QArrayData::AllocationOption>) (in /usr/lib
        /x86_64-linux-gnu/libQt5Core.so.5.15.13)
        ==21831==    by 0x578AC1B: QString::reallocData(unsigned int, bool) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.15.13)
        ==21831==    by 0x519EB2E: QTextBlock::text() const (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.15.13)
        ==21831==    by 0x520CC9A: ??? (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.15.13)
        ==21831==    by 0x520CE5F: ??? (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.15.13)
        ==21831==    by 0x5938DBE: ??? (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.15.13)
        ==21831==    by 0x51A1F4F: QTextDocument::contentsChange(int, int, int) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.15.13)
        ==21831==    by 0x51BA780: QTextDocumentPrivate::finishEdit() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.15.13)
        ==21831==    by 0x51F326A: QTextCursor::insertText(QString const&, QTextCharFormat const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.15.13)
        ==21831==    by 0x51F431F: QTextCursor::insertText(QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.15.13)
        ==21831==    by 0x114AF3: Mode::createFile() (createfile.cpp:20)
        ==21831== 
        ==21831== Conditional jump or move depends on uninitialised value(s)
        ==21831==    at 0xC46633E: ???
        ==21831==    by 0xB1B5DF7: ???
        

        45b85dda-35c4-40ff-94c6-46c3d4e6c4ac-Screenshot from 2025-07-10 11-45-51.png
        (Pic to check line number)

        Any idea on why this is happening?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote last edited by
        #10

        @TON-618
        P.S.
        As a total aside, rather than reading line by line from the file and sending a line at a time to insertPlainText() you might find it's faster to just do a single

        ui.iTextEdit->insertPlainText(QString::fromUtf8(file.readAll()));
        

        Though I probably would not do that if the file is likely to be, say, 1GB+ in size....

        1 Reply Last reply
        0
        • T TON 618

          @JonB thats what i get if i run the program you provided. Thats probably normal looking at the valgrind error. But yeah, at least i'm not getting the errors i get in my program. I'll surely upgrade to the newest version of qt and see what happens anyway since code might change the output in some ways.

          ==39617== realloc() with size 0
          ==39617==    at 0x484DB80: realloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
          ==39617==    by 0xD94DC1F: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.550.144.03)
          ==39617==    by 0xCDEB986: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
          ==39617==    by 0xCE456D1: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
          ==39617==    by 0xCDEB012: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
          ==39617==    by 0x4005823: call_init (dl-init.c:120)
          ==39617==    by 0x4005823: _dl_init (dl-init.c:121)
          ==39617==    by 0x40015B1: _dl_catch_exception (dl-catch.c:211)
          ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:829)
          ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:792)
          ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
          ==39617==    by 0x400D163: _dl_open (dl-open.c:905)
          ==39617==    by 0x5EDF1A3: dlopen_doit (dlopen.c:56)
          ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
          ==39617==  Address 0x8d26140 is 0 bytes after a block of size 0 alloc'd
          ==39617==    at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
          ==39617==    by 0xD94DC0F: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.550.144.03)
          ==39617==    by 0xCDEB986: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
          ==39617==    by 0xCE456D1: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
          ==39617==    by 0xCDEB012: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
          ==39617==    by 0x4005823: call_init (dl-init.c:120)
          ==39617==    by 0x4005823: _dl_init (dl-init.c:121)
          ==39617==    by 0x40015B1: _dl_catch_exception (dl-catch.c:211)
          ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:829)
          ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:792)
          ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
          ==39617==    by 0x400D163: _dl_open (dl-open.c:905)
          ==39617==    by 0x5EDF1A3: dlopen_doit (dlopen.c:56)
          ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
          ==39617== 
          ==39617== posix_memalign() invalid size value: 0
          ==39617==    at 0x484E2E8: posix_memalign (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
          ==39617==    by 0xD94DC55: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.550.144.03)
          ==39617==    by 0xCDEB986: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
          ==39617==    by 0xCE456D1: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
          ==39617==    by 0xCDEB012: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_nvidia.so.550.144.03)
          ==39617==    by 0x4005823: call_init (dl-init.c:120)
          ==39617==    by 0x4005823: _dl_init (dl-init.c:121)
          ==39617==    by 0x40015B1: _dl_catch_exception (dl-catch.c:211)
          ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:829)
          ==39617==    by 0x400CD7B: dl_open_worker (dl-open.c:792)
          ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
          ==39617==    by 0x400D163: _dl_open (dl-open.c:905)
          ==39617==    by 0x5EDF1A3: dlopen_doit (dlopen.c:56)
          ==39617==    by 0x400151B: _dl_catch_exception (dl-catch.c:237)
          ==39617== 
          ==39617== 
          ==39617== HEAP SUMMARY:
          ==39617==     in use at exit: 145,595 bytes in 1,080 blocks
          ==39617==   total heap usage: 136,759 allocs, 135,679 frees, 2,985,737,013 bytes allocated
          ==39617== 
          ==39617== LEAK SUMMARY:
          ==39617==    definitely lost: 176 bytes in 4 blocks
          ==39617==    indirectly lost: 0 bytes in 0 blocks
          ==39617==      possibly lost: 368 bytes in 1 blocks
          ==39617==    still reachable: 145,051 bytes in 1,075 blocks
          ==39617==         suppressed: 0 bytes in 0 blocks
          ==39617== Rerun with --leak-check=full to see details of leaked memory
          ==39617== 
          ==39617== For lists of detected and suppressed errors, rerun with: -s
          ==39617== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote last edited by
          #11

          @TON-618
          This is not the format I see in any output. I just use Analyze > Valgrind Memory Analyzer from within Qt Creator. What are you doing?

          T 1 Reply Last reply
          0
          • T Offline
            T Offline
            TON 618
            wrote last edited by
            #12

            valgrind ./program_name
            Anyway even after changing qt version (6.9.1) i still get the same error, but it was something that had to be done. So not bad anyway, guess its something with my code at this point, even tho i'm just loading a file. I'll update this thread if i find a solution

            JonBJ 1 Reply Last reply
            0
            • T TON 618

              valgrind ./program_name
              Anyway even after changing qt version (6.9.1) i still get the same error, but it was something that had to be done. So not bad anyway, guess its something with my code at this point, even tho i'm just loading a file. I'll update this thread if i find a solution

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote last edited by JonB
              #13

              @TON-618
              Use the valgrind interface in Creator (assuming you are using that!), it's more integrated than external command line and suppresses external errors of which there are a few (6) which will always happen. Your last valgrind output looks like external stuff which should be ignored. Good luck with Qt :)

              T 1 Reply Last reply
              1
              • JonBJ JonB

                @TON-618
                Use the valgrind interface in Creator (assuming you are using that!), it's more integrated than external command line and suppresses external errors of which there are a few (6) which will always happen. Your last valgrind output looks like external stuff which should be ignored. Good luck with Qt :)

                T Offline
                T Offline
                TON 618
                wrote last edited by
                #14

                @JonB Thanks, i'll keep that in mind, didn't know that :)

                1 Reply Last reply
                0
                • JonBJ JonB

                  @TON-618
                  This is not the format I see in any output. I just use Analyze > Valgrind Memory Analyzer from within Qt Creator. What are you doing?

                  T Offline
                  T Offline
                  TON 618
                  wrote last edited by
                  #15

                  @JonB said in insertPlainText valgrind error.:

                  Analyze > Valgrind Memory Analyzer

                  Okay, i tried this and the error i was getting is not there anymore.
                  I just have some leaks and a conditional jump.

                  Qt's valgrind command is definitely more complex than mine and is probably suppressing the errors i was getting.

                  "valgrind --child-silent-after-fork=yes --xml-socket=127.0.0.1:39305 --log-so
                  cket=127.0.0.1:39049 --xml=yes --smc-check=stack --tool=memcheck --gen-suppressions=all --track-origins=yes --leak-check=summary --num-callers=
                  25 ./miniVim ../../srcs/main.cpp "
                  
                  JonBJ 1 Reply Last reply
                  1
                  • T TON 618

                    @JonB said in insertPlainText valgrind error.:

                    Analyze > Valgrind Memory Analyzer

                    Okay, i tried this and the error i was getting is not there anymore.
                    I just have some leaks and a conditional jump.

                    Qt's valgrind command is definitely more complex than mine and is probably suppressing the errors i was getting.

                    "valgrind --child-silent-after-fork=yes --xml-socket=127.0.0.1:39305 --log-so
                    cket=127.0.0.1:39049 --xml=yes --smc-check=stack --tool=memcheck --gen-suppressions=all --track-origins=yes --leak-check=summary --num-callers=
                    25 ./miniVim ../../srcs/main.cpp "
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote last edited by
                    #16

                    @TON-618
                    Yes, that is why I suggested you use the Creator one with whatever options it passes to valgrind, as (we presume!) they know what they are doing. I suggest you use this always, or at least look at the command line options it passes for your own use.

                    Good that this makes your reported leak --- which I don't think ever existed/there is nothing you can do about it --- go away. It's nice to keep your valgrind runs totally clean :)

                    T 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @TON-618
                      Yes, that is why I suggested you use the Creator one with whatever options it passes to valgrind, as (we presume!) they know what they are doing. I suggest you use this always, or at least look at the command line options it passes for your own use.

                      Good that this makes your reported leak --- which I don't think ever existed/there is nothing you can do about it --- go away. It's nice to keep your valgrind runs totally clean :)

                      T Offline
                      T Offline
                      TON 618
                      wrote last edited by
                      #17

                      @JonB Definitely is, thanks again for the help Jon

                      1 Reply Last reply
                      0
                      • T TON 618 has marked this topic as solved

                      • Login

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