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. Problem with deleting QBuffer
Forum Updated to NodeBB v4.3 + New Features

Problem with deleting QBuffer

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 1.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.
  • A Offline
    A Offline
    aiphae
    wrote on 14 Nov 2024, 18:47 last edited by
    #1

    I have a FileHandler class to handle simple file operations (mainly .csv). I need it to be able to load files both from path (as QFile) and file data (as QBuffer). Here's the crucial part of the class:

    #ifndef FILEHANDLER_H
    #define FILEHANDLER_H
    
    #include <QFile>
    #include <QBuffer>
    #include <QTextStream>
    #include <QDir>
    
    class FileHandler
    {
    public:
        FileHandler(const QString &path);
        FileHandler(const QByteArray &data, const QString &path = "");
        ~FileHandler();
    
    private:
        QFile *file = nullptr;
        QBuffer *buffer = nullptr;
        QTextStream in; // For writing into a file
        QTextStream out; // For reading from a file
        QString path;
        bool useBuffer = false;
    };
    
    #endif // FILEHANDLER_H
    
    

    FileHandler.cpp:

    FileHandler::FileHandler(const QString &path)
        : path(path)
        , useBuffer(false)
    {
        file = new QFile(path);
        out.setDevice(file);
        in.setDevice(file);
    }
    
    FileHandler::FileHandler(const QByteArray &data, const QString &path)
        : path(path)
        , useBuffer(true)
    {
        buffer = new QBuffer();
        buffer->setData(data);
        out.setDevice(buffer);
        in.setDevice(buffer);
    }
    
    FileHandler::~FileHandler() {
        delete file;
        delete buffer;
    }
    

    In my widget I have the following code:

    QString filename = QFileDialog::getOpenFileName(this, "Open File", QDir::homePath(), filter);
    // Create a temporary handler to check if the file can be opened 
    FileHandler *newHandler = nullptr;
    if (filename.endsWith(".zip", Qt::CaseInsensitive)) {
        // Reading .csv from an archive
        // ...
        // ...
        newHandler = new FileHandler(file.readAll(), zipFilename);
    }
    else {
        // If .csv file was selected
        newHandler = new FileHandler(filename);
    }
    
    if (csvHandler) {
        csvHandler->close();
        csvHandler->clear();
    }
    delete csvHandler;
    
    csvHandler = newHandler;
    

    The problem arises only when I'm dealing with a FileHandler constructed with QByteArray, so when FileHandler has QBuffer initialized. The program crashes at the line delete csvHandler; with the "is_block_type_valid(header->_block_use)" OR "_crtisvalidheappointer(block)".

    It DOES NOT happen when a FileHandler is constructed just with filepath. So as I understood, there is something wrong with deleteting QBuffer().

    The archive processing works well and there is absolutely nothing wrong with it.

    1 Reply Last reply
    0
    • A aiphae
      14 Nov 2024, 20:35

      @Christian-Ehrlicher said in Problem with deleting QBuffer:

      You are most likely mixing debug and release libs - I would guess you link your debug app against Qt release libs. This is not supported on windows due to different msvc runtimes.

      Is there a workaround?

      I tried this on a release version and it's working well. But what if I want to debug it?

      P Offline
      P Offline
      Pl45m4
      wrote on 15 Nov 2024, 04:46 last edited by
      #17

      @aiphae said in Problem with deleting QBuffer:

      Is there a workaround?

      I tried this on a release version and it's working well. But what if I want to debug it?

      Simple: Link you release build to Qt release libs and your app's debug build to Qt's debug libs.

      How did you install Qt?
      You should have them.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      A 1 Reply Last reply 15 Nov 2024, 07:53
      2
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 14 Nov 2024, 18:58 last edited by
        #2

        Where do you initialize file and buffer in the ctors? You currently only initialize them in one of the ctors.
        Also I don't see a reason why QFile and QBuffer should be created with new instead using plain objects in the class.

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

        S A 2 Replies Last reply 14 Nov 2024, 19:00
        0
        • C Christian Ehrlicher
          14 Nov 2024, 18:58

          Where do you initialize file and buffer in the ctors? You currently only initialize them in one of the ctors.
          Also I don't see a reason why QFile and QBuffer should be created with new instead using plain objects in the class.

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Nov 2024, 19:00 last edited by
          #3

          @Christian-Ehrlicher they are initialized at declaration time to nullptr.

          That said, there's indeed no use for pointers here.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply 14 Nov 2024, 19:06
          0
          • C Christian Ehrlicher
            14 Nov 2024, 18:58

            Where do you initialize file and buffer in the ctors? You currently only initialize them in one of the ctors.
            Also I don't see a reason why QFile and QBuffer should be created with new instead using plain objects in the class.

            A Offline
            A Offline
            aiphae
            wrote on 14 Nov 2024, 19:00 last edited by aiphae
            #4

            @Christian-Ehrlicher said in Problem with deleting QBuffer:

            Where do you initialize file and buffer in the ctors? You currently only initialize them in one of the ctors.
            Also I don't see a reason why QFile and QBuffer should be created with new instead using plain objects in the class.

            I want the class to handle EITHER QFile OR QBuffer. Because of this, I decided to make them pointers to save some memory.

            UPD: I remade the code so it doesn't use pointers in the FileHandler class. It didn't help. Still the same error.

            C 1 Reply Last reply 14 Nov 2024, 19:06
            0
            • S SGaist
              14 Nov 2024, 19:00

              @Christian-Ehrlicher they are initialized at declaration time to nullptr.

              That said, there's indeed no use for pointers here.

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 14 Nov 2024, 19:06 last edited by Christian Ehrlicher
              #5

              @SGaist said in Problem with deleting QBuffer:

              they are initialized at declaration time to nullptr.

              Where do you see this?
              Now there is also the header... hmm

              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
              • A aiphae
                14 Nov 2024, 19:00

                @Christian-Ehrlicher said in Problem with deleting QBuffer:

                Where do you initialize file and buffer in the ctors? You currently only initialize them in one of the ctors.
                Also I don't see a reason why QFile and QBuffer should be created with new instead using plain objects in the class.

                I want the class to handle EITHER QFile OR QBuffer. Because of this, I decided to make them pointers to save some memory.

                UPD: I remade the code so it doesn't use pointers in the FileHandler class. It didn't help. Still the same error.

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 14 Nov 2024, 19:06 last edited by
                #6

                @aiphae said in Problem with deleting QBuffer:

                . Still the same error.

                Please provide some minimal, compilable code and a proper backtrace.

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

                A 1 Reply Last reply 14 Nov 2024, 19:15
                0
                • C Christian Ehrlicher
                  14 Nov 2024, 19:06

                  @aiphae said in Problem with deleting QBuffer:

                  . Still the same error.

                  Please provide some minimal, compilable code and a proper backtrace.

                  A Offline
                  A Offline
                  aiphae
                  wrote on 14 Nov 2024, 19:15 last edited by aiphae
                  #7

                  @Christian-Ehrlicher said in Problem with deleting QBuffer:

                  @aiphae said in Problem with deleting QBuffer:

                  . Still the same error.

                  Please provide some minimal, compilable code and a proper backtrace.

                  Maybe this:

                  FileHandler *csvHandler = nullptr;
                  QVector<QString> files = {"path_1", "path_2"};
                  
                  for (const auto &path: files) {
                      FileHandler *newHandler = nullptr;
                      newHandler = new FileHandler(QFile(path).readAll);
                      delete csvHandler;
                      csvHandler = newHandler;
                  }
                  

                  FileHandler.h and FileHandler.cpp are above.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 14 Nov 2024, 19:23 last edited by
                    #8

                    This does not crash for me:

                    int main(int argc, char** argv)
                    {
                        QApplication a(argc, argv);
                        FileHandler* csvHandler = nullptr;
                        QVector<QString> files = { "existing_file1.txt", "existing_file2.txt" };
                        for (const auto& path : files) {
                            QFile f(path);
                            f.open(QIODevice::ReadOnly);
                            auto newHandler = new FileHandler(f.readAll());
                            delete csvHandler;
                            csvHandler = newHandler;
                        }
                        delete csvHandler;
                        return 0;
                    }
                    

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

                    A 1 Reply Last reply 14 Nov 2024, 19:34
                    0
                    • C Christian Ehrlicher
                      14 Nov 2024, 19:23

                      This does not crash for me:

                      int main(int argc, char** argv)
                      {
                          QApplication a(argc, argv);
                          FileHandler* csvHandler = nullptr;
                          QVector<QString> files = { "existing_file1.txt", "existing_file2.txt" };
                          for (const auto& path : files) {
                              QFile f(path);
                              f.open(QIODevice::ReadOnly);
                              auto newHandler = new FileHandler(f.readAll());
                              delete csvHandler;
                              csvHandler = newHandler;
                          }
                          delete csvHandler;
                          return 0;
                      }
                      
                      A Offline
                      A Offline
                      aiphae
                      wrote on 14 Nov 2024, 19:34 last edited by aiphae
                      #9

                      @Christian-Ehrlicher

                      That's interesting. In my code I tried to replace newHandler = new FileHandler(file.readAll(), zipFilename); with newHandler = new FileHandler(file.readAll()); (without specifying a filename). No crash now. So the issue is not in QBuffer.

                      zipFilename is QString zipFilename = zip.getCurrentFileName();. I don't understand what's wrong...

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        aiphae
                        wrote on 14 Nov 2024, 20:00 last edited by aiphae
                        #10

                        @Christian-Ehrlicher

                        Please try this:

                        int main(int argc, char** argv)
                        {
                            QApplication a(argc, argv);
                            FileHandler* csvHandler = nullptr;
                            QVector<QString> files = { "existing_file1.txt", "existing_file2.txt" };
                            for (const auto& path : files) {
                                QFile f(path);
                                f.open(QIODevice::ReadOnly);
                                auto newHandler = new FileHandler(f.readAll(), path);
                                delete csvHandler;
                                csvHandler = newHandler;
                            }
                            delete csvHandler;
                            return 0;
                        }
                        

                        In this case the error should appear.

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 14 Nov 2024, 20:04 last edited by
                          #11

                          No crash here and I also don't see a reason why it should crash.

                          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
                          • A Offline
                            A Offline
                            aiphae
                            wrote on 14 Nov 2024, 20:13 last edited by
                            #12

                            Well it crashes when I'm specifying path and it doesn't when I don't... It's weird.

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 14 Nov 2024, 20:16 last edited by
                              #13

                              As I said before - please post the stacktrace.

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

                              A 1 Reply Last reply 14 Nov 2024, 20:24
                              0
                              • C Christian Ehrlicher
                                14 Nov 2024, 20:16

                                As I said before - please post the stacktrace.

                                A Offline
                                A Offline
                                aiphae
                                wrote on 14 Nov 2024, 20:24 last edited by
                                #14

                                @Christian-Ehrlicher

                                This?
                                e52fc7ec-9108-4e42-aa14-ea8c4e746287-image.png

                                1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 14 Nov 2024, 20:28 last edited by
                                  #15

                                  You are most likely mixing debug and release libs - I would guess you link your debug app against Qt release libs. This is not supported on windows due to different msvc runtimes.

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

                                  A 1 Reply Last reply 14 Nov 2024, 20:35
                                  3
                                  • C Christian Ehrlicher
                                    14 Nov 2024, 20:28

                                    You are most likely mixing debug and release libs - I would guess you link your debug app against Qt release libs. This is not supported on windows due to different msvc runtimes.

                                    A Offline
                                    A Offline
                                    aiphae
                                    wrote on 14 Nov 2024, 20:35 last edited by
                                    #16

                                    @Christian-Ehrlicher said in Problem with deleting QBuffer:

                                    You are most likely mixing debug and release libs - I would guess you link your debug app against Qt release libs. This is not supported on windows due to different msvc runtimes.

                                    Is there a workaround?

                                    I tried this on a release version and it's working well. But what if I want to debug it?

                                    P 1 Reply Last reply 15 Nov 2024, 04:46
                                    0
                                    • A aiphae
                                      14 Nov 2024, 20:35

                                      @Christian-Ehrlicher said in Problem with deleting QBuffer:

                                      You are most likely mixing debug and release libs - I would guess you link your debug app against Qt release libs. This is not supported on windows due to different msvc runtimes.

                                      Is there a workaround?

                                      I tried this on a release version and it's working well. But what if I want to debug it?

                                      P Offline
                                      P Offline
                                      Pl45m4
                                      wrote on 15 Nov 2024, 04:46 last edited by
                                      #17

                                      @aiphae said in Problem with deleting QBuffer:

                                      Is there a workaround?

                                      I tried this on a release version and it's working well. But what if I want to debug it?

                                      Simple: Link you release build to Qt release libs and your app's debug build to Qt's debug libs.

                                      How did you install Qt?
                                      You should have them.


                                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                      ~E. W. Dijkstra

                                      A 1 Reply Last reply 15 Nov 2024, 07:53
                                      2
                                      • P Pl45m4
                                        15 Nov 2024, 04:46

                                        @aiphae said in Problem with deleting QBuffer:

                                        Is there a workaround?

                                        I tried this on a release version and it's working well. But what if I want to debug it?

                                        Simple: Link you release build to Qt release libs and your app's debug build to Qt's debug libs.

                                        How did you install Qt?
                                        You should have them.

                                        A Offline
                                        A Offline
                                        aiphae
                                        wrote on 15 Nov 2024, 07:53 last edited by
                                        #18

                                        @Pl45m4 said in Problem with deleting QBuffer:

                                        Simple: Link you release build to Qt release libs and your app's debug build to Qt's debug libs.

                                        Yes. Apparently I was linking external release libraries instead of debug ones. The issue is solved. Thank you!

                                        1 Reply Last reply
                                        0
                                        • A aiphae has marked this topic as solved on 15 Nov 2024, 07:53

                                        1/18

                                        14 Nov 2024, 18:47

                                        • Login

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