Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Read big file and store the data into QVector<QByteArray> will cause std::bad_alloc error
Qt 6.12 LTS is out! See what's new in the release blog

Read big file and store the data into QVector<QByteArray> will cause std::bad_alloc error

Scheduled Pinned Locked Moved Unsolved General and Desktop
35 Posts 4 Posters 10.5k 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.
  • S Offline
    S Offline
    saerpa
    wrote on last edited by
    #1

    hi everyone,

    I try to read a file and store the data in the variable.

    it's OK when the input file is small but when the file is big (more than 50,000 lines), it will cause std::bad_alloc error in the specific build Qt_5_15_2_MinGW_32_bit.

    My test code is really simple:

    #include "mainwindow.h"
    #include <QByteArray>
    #include <QDebug>
    #include <QFile>
    #include <QFileDialog>
    #include <QVector>
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QVector<QByteArray> fileBuffer;
        QString fileName = QFileDialog::getOpenFileName(this, "open file", "./", "*.*");
        QFile file;
        file.setFileName(fileName);
    
        if (file.open(QIODevice::ReadOnly)) {
            while (!file.atEnd()) {
                QByteArray line = file.readLine();
    
                fileBuffer.append(line);
            }
        }
    
        qDebug() << "Read lines: " << fileBuffer.length();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    Output of build Qt_5_15_2_MinGW_32_bit:

    19:17:30: Starting C:\Users\alex\Documents\build-test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\test.exe...
    terminate called after throwing an instance of 'std::bad_alloc'
      what():  std::bad_alloc
    

    Output of build Qt_5_15_2_MinGW_64_bit:

    19:17:49: Starting C:\Users\alex\Documents\build-test-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\test.exe...
    Read lines:  70572
    

    By the way, it seems MinGW 8.1.0 32-bit in Qt 5.15.2 is the latest 32-bit build and no longer support offically?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • S saerpa

      hi everyone,

      I try to read a file and store the data in the variable.

      it's OK when the input file is small but when the file is big (more than 50,000 lines), it will cause std::bad_alloc error in the specific build Qt_5_15_2_MinGW_32_bit.

      My test code is really simple:

      #include "mainwindow.h"
      #include <QByteArray>
      #include <QDebug>
      #include <QFile>
      #include <QFileDialog>
      #include <QVector>
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          QVector<QByteArray> fileBuffer;
          QString fileName = QFileDialog::getOpenFileName(this, "open file", "./", "*.*");
          QFile file;
          file.setFileName(fileName);
      
          if (file.open(QIODevice::ReadOnly)) {
              while (!file.atEnd()) {
                  QByteArray line = file.readLine();
      
                  fileBuffer.append(line);
              }
          }
      
          qDebug() << "Read lines: " << fileBuffer.length();
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      

      Output of build Qt_5_15_2_MinGW_32_bit:

      19:17:30: Starting C:\Users\alex\Documents\build-test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\test.exe...
      terminate called after throwing an instance of 'std::bad_alloc'
        what():  std::bad_alloc
      

      Output of build Qt_5_15_2_MinGW_64_bit:

      19:17:49: Starting C:\Users\alex\Documents\build-test-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\test.exe...
      Read lines:  70572
      

      By the way, it seems MinGW 8.1.0 32-bit in Qt 5.15.2 is the latest 32-bit build and no longer support offically?

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

      @saerpa said in Read big file and store the data into QVector<QByteArray> will cause std::bad_alloc error:

      but when the file is big (more than 50,000 lines)

      How big is the file (in bytes)?
      Don't load whole file into memory if it is way too big...

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

      S 1 Reply Last reply
      0
      • S saerpa

        hi everyone,

        I try to read a file and store the data in the variable.

        it's OK when the input file is small but when the file is big (more than 50,000 lines), it will cause std::bad_alloc error in the specific build Qt_5_15_2_MinGW_32_bit.

        My test code is really simple:

        #include "mainwindow.h"
        #include <QByteArray>
        #include <QDebug>
        #include <QFile>
        #include <QFileDialog>
        #include <QVector>
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            QVector<QByteArray> fileBuffer;
            QString fileName = QFileDialog::getOpenFileName(this, "open file", "./", "*.*");
            QFile file;
            file.setFileName(fileName);
        
            if (file.open(QIODevice::ReadOnly)) {
                while (!file.atEnd()) {
                    QByteArray line = file.readLine();
        
                    fileBuffer.append(line);
                }
            }
        
            qDebug() << "Read lines: " << fileBuffer.length();
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        

        Output of build Qt_5_15_2_MinGW_32_bit:

        19:17:30: Starting C:\Users\alex\Documents\build-test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\test.exe...
        terminate called after throwing an instance of 'std::bad_alloc'
          what():  std::bad_alloc
        

        Output of build Qt_5_15_2_MinGW_64_bit:

        19:17:49: Starting C:\Users\alex\Documents\build-test-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\test.exe...
        Read lines:  70572
        

        By the way, it seems MinGW 8.1.0 32-bit in Qt 5.15.2 is the latest 32-bit build and no longer support offically?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @saerpa
        Since you are Windows and calling readLine() you should include flag QIODevice::Text when opening the file. Probably not relevant to issue here but if you intend to use this code you ought do so, just in case it makes any difference.

        S 1 Reply Last reply
        0
        • jsulmJ jsulm

          @saerpa said in Read big file and store the data into QVector<QByteArray> will cause std::bad_alloc error:

          but when the file is big (more than 50,000 lines)

          How big is the file (in bytes)?
          Don't load whole file into memory if it is way too big...

          S Offline
          S Offline
          saerpa
          wrote on last edited by
          #4

          @jsulm In my case it's a .s19 file: 3.23MB, 70573 lines.
          I try to load it into memory first and parse it line by line.

          jsulmJ 1 Reply Last reply
          0
          • JonBJ JonB

            @saerpa
            Since you are Windows and calling readLine() you should include flag QIODevice::Text when opening the file. Probably not relevant to issue here but if you intend to use this code you ought do so, just in case it makes any difference.

            S Offline
            S Offline
            saerpa
            wrote on last edited by
            #5

            @JonB I add the flag and try it again, the issue still exists.
            Thanks for the advice! It's really important for me, now I know this flag is important :)

            Christian EhrlicherC 1 Reply Last reply
            0
            • S saerpa

              @JonB I add the flag and try it again, the issue still exists.
              Thanks for the advice! It's really important for me, now I know this flag is important :)

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I doubt this app will crash due to reading 3.5mb

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              S 1 Reply Last reply
              0
              • S saerpa

                @jsulm In my case it's a .s19 file: 3.23MB, 70573 lines.
                I try to load it into memory first and parse it line by line.

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

                @saerpa said in Read big file and store the data into QVector<QByteArray> will cause std::bad_alloc error:

                3.23MB, 70573 lines

                This is not much.
                Run your app through debugger and post the stack trace after the crash.

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

                S 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @saerpa said in Read big file and store the data into QVector<QByteArray> will cause std::bad_alloc error:

                  3.23MB, 70573 lines

                  This is not much.
                  Run your app through debugger and post the stack trace after the crash.

                  S Offline
                  S Offline
                  saerpa
                  wrote on last edited by
                  #8

                  @jsulm

                  15:08:38: Debugging C:\Users\alex\Documents\build-test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\test.exe ...
                  onecore\windows\directx\database\helperlibrary\lib\perappusersettingsqueryimpl.cpp(121)\d3d9.dll!686B0C7E: (caller: 686766B4) ReturnHr(1) tid(2620) 8000FFFF �����Թ���
                  onecore\windows\directx\database\helperlibrary\lib\perappusersettingsqueryimpl.cpp(98)\d3d9.dll!686B0BE5: (caller: 6867658C) ReturnHr(2) tid(2620) 8000FFFF �����Թ���
                  onecore\windows\directx\database\helperlibrary\lib\directxdatabasehelper.cpp(1410)\d3d9.dll!68676686: (caller: 68674DE6) ReturnHr(3) tid(2620) 8000FFFF �����Թ���
                  terminate called after throwing an instance of 'std::bad_alloc'
                    what():  std::bad_alloc
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • S saerpa

                    @jsulm

                    15:08:38: Debugging C:\Users\alex\Documents\build-test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\test.exe ...
                    onecore\windows\directx\database\helperlibrary\lib\perappusersettingsqueryimpl.cpp(121)\d3d9.dll!686B0C7E: (caller: 686766B4) ReturnHr(1) tid(2620) 8000FFFF �����Թ���
                    onecore\windows\directx\database\helperlibrary\lib\perappusersettingsqueryimpl.cpp(98)\d3d9.dll!686B0BE5: (caller: 6867658C) ReturnHr(2) tid(2620) 8000FFFF �����Թ���
                    onecore\windows\directx\database\helperlibrary\lib\directxdatabasehelper.cpp(1410)\d3d9.dll!68676686: (caller: 68674DE6) ReturnHr(3) tid(2620) 8000FFFF �����Թ���
                    terminate called after throwing an instance of 'std::bad_alloc'
                      what():  std::bad_alloc
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @saerpa This does not look like a crash in your code. Does your app also crash if you do not read that file?

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

                    Christian EhrlicherC S 2 Replies Last reply
                    0
                    • jsulmJ jsulm

                      @saerpa This does not look like a crash in your code. Does your app also crash if you do not read that file?

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @jsulm what you see is no stack trace.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      jsulmJ 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        @jsulm what you see is no stack trace.

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

                        @Christian-Ehrlicher Yeah, you're right

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

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          I doubt this app will crash due to reading 3.5mb

                          S Offline
                          S Offline
                          saerpa
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher Your are right! How could you know?

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • S saerpa

                            @Christian-Ehrlicher Your are right! How could you know?

                            Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @saerpa said in Read big file and store the data into QVector<QByteArray> will cause std::bad_alloc error:

                            How could you know?

                            What do you mean? It's no stack trace. Please provide a proper one so we can take a look on it.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @saerpa This does not look like a crash in your code. Does your app also crash if you do not read that file?

                              S Offline
                              S Offline
                              saerpa
                              wrote on last edited by
                              #14

                              @jsulm Something wrong in this line:

                              fileBuffer.append(line);
                              

                              If I comment this line, the app works fine.
                              And if the load file is less than 40,000 lines, it works fine too.
                              The content of the file doesn't matter, I tested with other files.

                              Something wrong with QVector, it's OK in 64-bit build and NG in 32-bit build.

                              jsulmJ 1 Reply Last reply
                              0
                              • S saerpa

                                @jsulm Something wrong in this line:

                                fileBuffer.append(line);
                                

                                If I comment this line, the app works fine.
                                And if the load file is less than 40,000 lines, it works fine too.
                                The content of the file doesn't matter, I tested with other files.

                                Something wrong with QVector, it's OK in 64-bit build and NG in 32-bit build.

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

                                @saerpa said in Read big file and store the data into QVector<QByteArray> will cause std::bad_alloc error:

                                NG in 32-bit build

                                Reading view MB of data into RAM is also fine on 32bit machine. I'm sure there is no problem with QVector. You still did not provide stack trace.

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

                                S 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @saerpa said in Read big file and store the data into QVector<QByteArray> will cause std::bad_alloc error:

                                  NG in 32-bit build

                                  Reading view MB of data into RAM is also fine on 32bit machine. I'm sure there is no problem with QVector. You still did not provide stack trace.

                                  S Offline
                                  S Offline
                                  saerpa
                                  wrote on last edited by
                                  #16

                                  @jsulm

                                  It won't trigger line 28 and exit.
                                  Snipaste_2023-10-18_16-26-20.png

                                  Snipaste_2023-10-18_16-27-11.png

                                  Christian EhrlicherC JonBJ 2 Replies Last reply
                                  0
                                  • S saerpa

                                    @jsulm

                                    It won't trigger line 28 and exit.
                                    Snipaste_2023-10-18_16-26-20.png

                                    Snipaste_2023-10-18_16-27-11.png

                                    Christian EhrlicherC Online
                                    Christian EhrlicherC Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by Christian Ehrlicher
                                    #17

                                    How long is one line? And how much lines are in the file?

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    S 1 Reply Last reply
                                    0
                                    • S saerpa

                                      @jsulm

                                      It won't trigger line 28 and exit.
                                      Snipaste_2023-10-18_16-26-20.png

                                      Snipaste_2023-10-18_16-27-11.png

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @saerpa
                                      Have you allowed it to crash in your screenshots, so we can see stack trace? It seems to me you have stopped it on a breakpoint at line #22. That is not what we are looking for.

                                      S 1 Reply Last reply
                                      2
                                      • Christian EhrlicherC Christian Ehrlicher

                                        How long is one line? And how much lines are in the file?

                                        S Offline
                                        S Offline
                                        saerpa
                                        wrote on last edited by
                                        #19

                                        @Christian-Ehrlicher

                                        3.23MB, 70573 lines.
                                        Tail of my file:

                                        S315A0133A3049F5200AD45F54F0D80CB700040EA60F8C
                                        S315A0133A40B71F01FF3C02C2F814F03700E10314219B
                                        S315A0133A503701E1137685DF00F8FFDF01F6FFF602E3
                                        S315A0133A60761382193C10D45F9130004FD9440C16AB
                                        S315A0133A7048346DFF8FCB9130004FD944300602F4F2
                                        S315A0133A806DFF88CBDF091180D8069130004FD9443A
                                        S315A0133A903C0654FFD80B53CF22F0D4FFD4FF10FF0C
                                        S311A0133AA048946DFF77CB02920090FFFFB5
                                        S6040113A93E
                                        S705A002004018
                                        
                                        Christian EhrlicherC 1 Reply Last reply
                                        0
                                        • S saerpa

                                          @Christian-Ehrlicher

                                          3.23MB, 70573 lines.
                                          Tail of my file:

                                          S315A0133A3049F5200AD45F54F0D80CB700040EA60F8C
                                          S315A0133A40B71F01FF3C02C2F814F03700E10314219B
                                          S315A0133A503701E1137685DF00F8FFDF01F6FFF602E3
                                          S315A0133A60761382193C10D45F9130004FD9440C16AB
                                          S315A0133A7048346DFF8FCB9130004FD944300602F4F2
                                          S315A0133A806DFF88CBDF091180D8069130004FD9443A
                                          S315A0133A903C0654FFD80B53CF22F0D4FFD4FF10FF0C
                                          S311A0133AA048946DFF77CB02920090FFFFB5
                                          S6040113A93E
                                          S705A002004018
                                          
                                          Christian EhrlicherC Online
                                          Christian EhrlicherC Online
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on last edited by Christian Ehrlicher
                                          #20

                                          Print out the line number for each line you read in to see at which line it is crashing. Then look at this specific line in your file. Maybe also print out the length of the read QByteArray.

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          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
                                          • Unsolved