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. .readAll succeed but Segmentation fault on ~QByteArray when function return
Forum Updated to NodeBB v4.3 + New Features

.readAll succeed but Segmentation fault on ~QByteArray when function return

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 747 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.
  • K KJ92

    I'm writing a function that read XML but when the function return i get segmentation fault on ~QByteArray. Here is the code snippet:

    bool xmlDoc(const string &filePath)
    {
        QFile file(QString::fromStdString(filePath));
        if (!file.open(QIODevice::ReadOnly))
        {
            return false;
        }
    
        QByteArray document = file.readAll();
        file.close();
    
        int options = pugi::parse_default;
        
        pugi::xml_document xmlDoc;
        pugi::xml_parse_result result = xmlDoc.load_buffer(
                                      document.constData(),
                                      document.size(),
                                      options);
        if (!result)
        {
            return false;
        }
    
        return true;
    }
    

    When function return true i get segmentation fault on this:

    #0  0x000055d8c4b206cd in std::__atomic_base<int>::fetch_sub (__m=std::memory_order_acq_rel, __i=1, this=0x7fff00000001)
    
        at /usr/include/c++/11/bits/atomic_base.h:646
    
    #1  QAtomicOps<int>::deref<int> (_q_value=<error reading variable: Cannot access memory at address 0x7fff00000001>)
    
        at /usr/lib/qt6/include/QtCore/qatomic_cxx11.h:265
    
    #2  0x000055d8c4b1f0b4 in QBasicAtomicInteger<int>::deref (this=0x7fff00000001) at /usr/lib/qt6/include/QtCore/qbasicatomic.h:66
    
    #3  0x000055d8c4b1eb04 in QArrayData::deref (this=0x7fff00000001) at /usr/lib/qt6/include/QtCore/qarraydata.h:58
    
    #4  0x000055d8c4b20751 in QArrayDataPointer<char>::deref (this=0x7fffc9abdd90) at /usr/lib/qt6/include/QtCore/qarraydatapointer.h:312
    
    #5  0x000055d8c4b1f278 in QArrayDataPointer<char>::~QArrayDataPointer (this=0x7fffc9abdd90, __in_chrg=<optimized out>)
    
        at /usr/lib/qt6/include/QtCore/qarraydatapointer.h:93
    
    #6  0x000055d8c4b1eba0 in QByteArray::~QByteArray (this=0x7fffc9abdd90, __in_chrg=<optimized out>) at /usr/lib/qt6/include/QtCore/qbytearray.h:472
    
    #7  0x000055d8c4b1bc54 in xmlDoc (filePath=":/pc/xml/my_xml.xml")
    

    Any idea what went wrong or how to debug this ?

    C Offline
    C Offline
    ChrisW67
    wrote on last edited by
    #2

    @KJ92 Pugixml can load directly from a file, so I am not sure why you use the intermediate QByteArray.

    Does it make any difference if you force the pugi::xml_document destruction before the return?

    ...
        int options = pugi::parse_default;
        pugi::xml_parse_result result;
        {
            pugi::xml_document xmlDoc;
            result = xmlDoc.load_buffer(
                                  document.constData(),
                                  document.size(),
                                  options);
        }
    ...
    
    JonBJ K 2 Replies Last reply
    0
    • C ChrisW67

      @KJ92 Pugixml can load directly from a file, so I am not sure why you use the intermediate QByteArray.

      Does it make any difference if you force the pugi::xml_document destruction before the return?

      ...
          int options = pugi::parse_default;
          pugi::xml_parse_result result;
          {
              pugi::xml_document xmlDoc;
              result = xmlDoc.load_buffer(
                                    document.constData(),
                                    document.size(),
                                    options);
          }
      ...
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

      @ChrisW67 said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

      @KJ92 Pugixml can load directly from a file, so I am not sure why you use the intermediate QByteArray.

      Stack trace shows

      #7 0x000055d8c4b1bc54 in xmlDoc (filePath=":/pc/xml/my_xml.xml")

      so OP is using a Qt resource file, hence can't ask Pugixml to open that! :)

      Does it make any difference if you force the pugi::xml_document destruction before the return?

      Yeah, so far as I can see existing code ought to do destruction in right/acceptable order, I wondered about that. Furthermore I looked up and pugixml load_buffer() says it copies the buffer content passed in, so don't know what is going with the QByteArray problem!

      I think I would start by verifying that if the xmlDoc.load_buffer() statement is commented out --- or pass some literal data in, not from the QByteArray document variable --- that all is well on the QByteArray destructor not erroring?

      C 1 Reply Last reply
      1
      • C ChrisW67

        @KJ92 Pugixml can load directly from a file, so I am not sure why you use the intermediate QByteArray.

        Does it make any difference if you force the pugi::xml_document destruction before the return?

        ...
            int options = pugi::parse_default;
            pugi::xml_parse_result result;
            {
                pugi::xml_document xmlDoc;
                result = xmlDoc.load_buffer(
                                      document.constData(),
                                      document.size(),
                                      options);
            }
        ...
        
        K Offline
        K Offline
        KJ92
        wrote on last edited by KJ92
        #4

        @ChrisW67 about

        Pugixml can load directly from a file, so I am not sure why you use the intermediate QByteArray.
        

        please see @JonB 's comment. its because the path is a QT resource

        about this,

        Does it make any difference if you force the pugi::xml_document destruction before the return?
        

        In my original code, I get the pugi::xml_document as variable. so its destruction is not possible as its used in the code.

        I think I would start by verifying that if the xmlDoc.load_buffer() statement is commented out --- or pass some literal data in, not from the QByteArray document variable --- that all is well on the QByteArray destructor not erroring?
        

        @JonB When i comment out xmlDoc.load_buffer() the code works.

        BTW, I'm trying to upgrade my QT from 5 to 6. This code snippet worked well in QT5.
        Now in QT6 its failing in QByteArray destructor.

        JonBJ J.HilkJ 2 Replies Last reply
        0
        • K KJ92

          @ChrisW67 about

          Pugixml can load directly from a file, so I am not sure why you use the intermediate QByteArray.
          

          please see @JonB 's comment. its because the path is a QT resource

          about this,

          Does it make any difference if you force the pugi::xml_document destruction before the return?
          

          In my original code, I get the pugi::xml_document as variable. so its destruction is not possible as its used in the code.

          I think I would start by verifying that if the xmlDoc.load_buffer() statement is commented out --- or pass some literal data in, not from the QByteArray document variable --- that all is well on the QByteArray destructor not erroring?
          

          @JonB When i comment out xmlDoc.load_buffer() the code works.

          BTW, I'm trying to upgrade my QT from 5 to 6. This code snippet worked well in QT5.
          Now in QT6 its failing in QByteArray destructor.

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

          @KJ92 said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

          In my original code, I get the pugi::xml_document as variable. so its destruction is not possible as its used in the code.

          That would be a possibly different situation. Are you saying that the code as you show presently, exactly that, produces the error?

          BTW, I'm trying to upgrade my QT from 5 to 6. This code snippet worked well in QT5.
          Now in QT6 its failing in QByteArray destructor.

          This sounds terribly relevant! Not that I know why, but someone....

          P.S.
          Have a try of document.data() instead of document.constData(), you should not have to and it ought make no difference but might be useful for investigation....

          K 1 Reply Last reply
          0
          • JonBJ JonB

            @KJ92 said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

            In my original code, I get the pugi::xml_document as variable. so its destruction is not possible as its used in the code.

            That would be a possibly different situation. Are you saying that the code as you show presently, exactly that, produces the error?

            BTW, I'm trying to upgrade my QT from 5 to 6. This code snippet worked well in QT5.
            Now in QT6 its failing in QByteArray destructor.

            This sounds terribly relevant! Not that I know why, but someone....

            P.S.
            Have a try of document.data() instead of document.constData(), you should not have to and it ought make no difference but might be useful for investigation....

            K Offline
            K Offline
            KJ92
            wrote on last edited by
            #6

            @JonB said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

            That would be a possibly different situation. Are you saying that the code as you show presently, exactly that, produces the error?

            yes . same code snippet fails as well.

            @JonB said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

            Have a try of document.data() instead of document.constData(), you should not have to and it ought make no difference but might be useful for investigation....

            same error

            1 Reply Last reply
            0
            • K KJ92

              @ChrisW67 about

              Pugixml can load directly from a file, so I am not sure why you use the intermediate QByteArray.
              

              please see @JonB 's comment. its because the path is a QT resource

              about this,

              Does it make any difference if you force the pugi::xml_document destruction before the return?
              

              In my original code, I get the pugi::xml_document as variable. so its destruction is not possible as its used in the code.

              I think I would start by verifying that if the xmlDoc.load_buffer() statement is commented out --- or pass some literal data in, not from the QByteArray document variable --- that all is well on the QByteArray destructor not erroring?
              

              @JonB When i comment out xmlDoc.load_buffer() the code works.

              BTW, I'm trying to upgrade my QT from 5 to 6. This code snippet worked well in QT5.
              Now in QT6 its failing in QByteArray destructor.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #7

              @KJ92 said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

              In my original code, I get the pugi::xml_document as variable. so its destruction is not possible as its used in the code.

              so, the code sample you showed is not accurate. Please post the actually code.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              K 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @KJ92 said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

                In my original code, I get the pugi::xml_document as variable. so its destruction is not possible as its used in the code.

                so, the code sample you showed is not accurate. Please post the actually code.

                K Offline
                K Offline
                KJ92
                wrote on last edited by
                #8

                @J-Hilk said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

                so, the code sample you showed is not accurate. Please post the actually code.

                Please see my comment

                @KJ92 said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

                yes . same code snippet fails as well.

                JonBJ 1 Reply Last reply
                0
                • K KJ92

                  @J-Hilk said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

                  so, the code sample you showed is not accurate. Please post the actually code.

                  Please see my comment

                  @KJ92 said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

                  yes . same code snippet fails as well.

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

                  @KJ92
                  You need to establish definitively whether it's the act of passing some QByteArray to xml_document.load_buffer() which is the cause. As opposed to anything else. The danger is somehow some sort of memory corruption going on, which makes tracking down bugs notoriously difficult.

                  Also as I said the Qt6 vs Qt5 looks significant to me.

                  Just for me, change the code fragment you have (the one you have been showing, not something else which is in your code) from

                  QByteArray document = file.readAll();
                  

                  to something like

                  QByteArray *document = new QByteArray(file.readAll());
                  

                  Now the QDocument is on the heap not the stack. Do not bother to delete/free it, yes I know it leaks. Does this make the problem go away? Presumably it does since no destructor gets called.

                  K 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @KJ92
                    You need to establish definitively whether it's the act of passing some QByteArray to xml_document.load_buffer() which is the cause. As opposed to anything else. The danger is somehow some sort of memory corruption going on, which makes tracking down bugs notoriously difficult.

                    Also as I said the Qt6 vs Qt5 looks significant to me.

                    Just for me, change the code fragment you have (the one you have been showing, not something else which is in your code) from

                    QByteArray document = file.readAll();
                    

                    to something like

                    QByteArray *document = new QByteArray(file.readAll());
                    

                    Now the QDocument is on the heap not the stack. Do not bother to delete/free it, yes I know it leaks. Does this make the problem go away? Presumably it does since no destructor gets called.

                    K Offline
                    K Offline
                    KJ92
                    wrote on last edited by
                    #10

                    @JonB said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

                    Now the QDocument is on the heap not the stack. Do not bother to delete/free it, yes I know it leaks. Does this make the problem go away? Presumably it does since no destructor gets called.

                    Yes the problem goes away. as a workaround i can add the heap allocation and then immedietly after

                     pugi::xml_parse_result result = xmlDoc.load_buffer(
                                                      document.constData(),
                                                      document.size(),
                                                      options);
                    

                    to delete.

                    But it's interesting why having it on the stack make this segmentation fault and the supposedly memory corruption upon resource release

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • K KJ92

                      @JonB said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

                      Now the QDocument is on the heap not the stack. Do not bother to delete/free it, yes I know it leaks. Does this make the problem go away? Presumably it does since no destructor gets called.

                      Yes the problem goes away. as a workaround i can add the heap allocation and then immedietly after

                       pugi::xml_parse_result result = xmlDoc.load_buffer(
                                                        document.constData(),
                                                        document.size(),
                                                        options);
                      

                      to delete.

                      But it's interesting why having it on the stack make this segmentation fault and the supposedly memory corruption upon resource release

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @KJ92 said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

                      But it's interesting why having it on the stack make this segmentation fault and the supposedly memory corruption upon resource release

                      This is just by accident. It doesn't matter if you create a QByteArray on the stack or on the heap, the real data is stored on the heap so document.data() always points to the heap. You have another problem - someone writes around in your stack who shouldn't. I would guess you're mixing debug and release build - e.g. a Qt debug build and a pugi release build or the other way round so the sizes of the objects don't match the compilers expectations.

                      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
                      2
                      • JonBJ JonB

                        @ChrisW67 said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

                        @KJ92 Pugixml can load directly from a file, so I am not sure why you use the intermediate QByteArray.

                        Stack trace shows

                        #7 0x000055d8c4b1bc54 in xmlDoc (filePath=":/pc/xml/my_xml.xml")

                        so OP is using a Qt resource file, hence can't ask Pugixml to open that! :)

                        Does it make any difference if you force the pugi::xml_document destruction before the return?

                        Yeah, so far as I can see existing code ought to do destruction in right/acceptable order, I wondered about that. Furthermore I looked up and pugixml load_buffer() says it copies the buffer content passed in, so don't know what is going with the QByteArray problem!

                        I think I would start by verifying that if the xmlDoc.load_buffer() statement is commented out --- or pass some literal data in, not from the QByteArray document variable --- that all is well on the QByteArray destructor not erroring?

                        C Offline
                        C Offline
                        ChrisW67
                        wrote on last edited by
                        #12

                        @JonB said in .readAll succeed but Segmentation fault on ~QByteArray when function return:

                        so OP is using a Qt resource file, hence can't ask Pugixml to open that! :)

                        Missed that

                        1 Reply Last reply
                        1

                        • Login

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