Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Strange behaviour with QString class members
Forum Updated to NodeBB v4.3 + New Features

Strange behaviour with QString class members

Scheduled Pinned Locked Moved Unsolved C++ Gurus
12 Posts 5 Posters 1.1k 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.
  • MesrineM Mesrine

    @Mesrine
    The solution is to define AddressBox::getAddressBech32() in the class.cpp file.

     QString AddressBox::getAddressBech32()const{ qDebug()<<"hey:";return m_bech32adddress; }
    

    But I do not get the reason.

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

    @Mesrine said in Strange behaviour with QString class members:

    But I do not get the reason.

    possibly the missing ,

    AddressBox::AddressBox(
    const QString hrp, QObject *parent):QObject(parent)
    , m_bech32adddress("WHATISWRONG")


    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.

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

      @Mesrine said in Strange behaviour with QString class members:

      But I do not get the reason.

      possibly the missing ,

      AddressBox::AddressBox(
      const QString hrp, QObject *parent):QObject(parent)
      , m_bech32adddress("WHATISWRONG")

      MesrineM Offline
      MesrineM Offline
      Mesrine
      wrote on last edited by
      #4

      @J-Hilk Now I do not get what you are trying to say :) .

      I have declared a member function on the definition of the class that just returns the QString member.
      The initialization of the const QString member is in the constructor.
      Even if I do not initialize the QString member the function should return a default QString object.

      kkoehneK 1 Reply Last reply
      0
      • MesrineM Mesrine

        @J-Hilk Now I do not get what you are trying to say :) .

        I have declared a member function on the definition of the class that just returns the QString member.
        The initialization of the const QString member is in the constructor.
        Even if I do not initialize the QString member the function should return a default QString object.

        kkoehneK Offline
        kkoehneK Offline
        kkoehne
        Moderators
        wrote on last edited by
        #5

        @Mesrine I agree that the code by itself shouldn't crash (though the nested qDebug() is suspicious, I still think it's behavior should be well defined).

        Anyhow, the code snippets you're showing is also not valid C++ (it misses at least a ';'). Try to create a minimal example that actually compiles. Also, do you get any stack traces?

        Kai

        Director R&D, The Qt Company

        MesrineM 1 Reply Last reply
        1
        • kkoehneK kkoehne

          @Mesrine I agree that the code by itself shouldn't crash (though the nested qDebug() is suspicious, I still think it's behavior should be well defined).

          Anyhow, the code snippets you're showing is also not valid C++ (it misses at least a ';'). Try to create a minimal example that actually compiles. Also, do you get any stack traces?

          Kai

          MesrineM Offline
          MesrineM Offline
          Mesrine
          wrote on last edited by
          #6

          @kkoehne
          Thanks, the code was just to get an idea. If I create a minimum reproducible example it works ok :).

          The runtime error was happening in a bigger project and it was fixed by passing the function definition from the headers to the sources.

          I published it here although it can not be reproducible just in case I was missing something or someone experienced before.

          1 Reply Last reply
          0
          • MesrineM Mesrine

            Hello everyone,

            I am having strange behavior randomly when using QString as a class member.

            For example
            class.hpp

            class AddressBox:public QObject
            {
            public:
                AddressBox(const QString hrp="rms",QObject *parent = nullptr);
              QString getAddressBech32()const{ 
            qDebug()<<"hey:";
            return m_bech32adddress; }
            }
            private:
            const QString m_bech32adddress
            }
            

            class.cpp

            #include"class.hpp"
            AddressBox::AddressBox(
                                   const QString hrp, QObject *parent):QObject(parent)
                m_bech32adddress("WHATISWRONG")
            {
                qDebug()<<"hello1:"<<m_bech32adddress;
                qDebug()<<"hello2:"<<getAddressBech32();
            };
            

            The output on a code that creates an AddressBox object is

            hello1: "WHATISWRONG"
            hey:
            Segmentation fault (core dumped)
            

            And every time I use getAddressBech32() it returns a Bad allocation or a Segmentation fault.

            I am initializing a const QString class member m_bech32adddress in the constructor of the class.
            Printing the value in the constructor and this is ok.
            But if call a function from the class it does not work.

            This was not happening before, and maybe if you try to reproduce it it won't happen. Something related happened to me before in a singleton class and has to set some members static.

            But there is some missing knowledge from my side of C++ on why this is happening.
            I am using qt6.7.0 dev I think.

            Any help will be appreciated.

            MesrineM Offline
            MesrineM Offline
            Mesrine
            wrote on last edited by
            #7

            @Mesrine

            If i do instead

            class.cpp

            #include"class.hpp"
            AddressBox::AddressBox(
                                   const QString hrp, QObject *parent):QObject(parent)
                m_bech32adddress("WHATISWRONG")
            {
              qDebug()<<"hello0:"<<m_bech32adddress.size();
                qDebug()<<"hello1:"<<getAddressBech32().size();
                qDebug()<<"hello2:"<<m_bech32adddress;
                qDebug()<<"hello3:"<<getAddressBech32();
            };
            

            the output is

            hello0: 11
            hello1: 94248499095840
            hello2: "WHATISWRONG"
            Segmentation fault (core dumped)
            
            J.HilkJ aha_1980A 2 Replies Last reply
            0
            • MesrineM Mesrine

              @Mesrine

              If i do instead

              class.cpp

              #include"class.hpp"
              AddressBox::AddressBox(
                                     const QString hrp, QObject *parent):QObject(parent)
                  m_bech32adddress("WHATISWRONG")
              {
                qDebug()<<"hello0:"<<m_bech32adddress.size();
                  qDebug()<<"hello1:"<<getAddressBech32().size();
                  qDebug()<<"hello2:"<<m_bech32adddress;
                  qDebug()<<"hello3:"<<getAddressBech32();
              };
              

              the output is

              hello0: 11
              hello1: 94248499095840
              hello2: "WHATISWRONG"
              Segmentation fault (core dumped)
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #8

              @Mesrine said in Strange behaviour with QString class members:

              AddressBox

              since file and class name differ so much, do you have multiple classes in those files ?


              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.

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

                @Mesrine said in Strange behaviour with QString class members:

                AddressBox

                since file and class name differ so much, do you have multiple classes in those files ?

                MesrineM Offline
                MesrineM Offline
                Mesrine
                wrote on last edited by
                #9

                @J-Hilk yes

                1 Reply Last reply
                0
                • MesrineM Mesrine

                  @Mesrine

                  If i do instead

                  class.cpp

                  #include"class.hpp"
                  AddressBox::AddressBox(
                                         const QString hrp, QObject *parent):QObject(parent)
                      m_bech32adddress("WHATISWRONG")
                  {
                    qDebug()<<"hello0:"<<m_bech32adddress.size();
                      qDebug()<<"hello1:"<<getAddressBech32().size();
                      qDebug()<<"hello2:"<<m_bech32adddress;
                      qDebug()<<"hello3:"<<getAddressBech32();
                  };
                  

                  the output is

                  hello0: 11
                  hello1: 94248499095840
                  hello2: "WHATISWRONG"
                  Segmentation fault (core dumped)
                  
                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @Mesrine said in Strange behaviour with QString class members:

                  Segmentation fault (core dumped)

                  Then run it in a debugger and inspect the stack trace (maybe post it here).

                  My feeling is, that you have some old object files that for some reason get linked but are incompatible to the rest of your code.

                  I'd clean all build artifacts and see if it reproduces after a clean build.

                  Regards.

                  Qt has to stay free or it will die.

                  Axel SpoerlA 1 Reply Last reply
                  1
                  • aha_1980A aha_1980

                    @Mesrine said in Strange behaviour with QString class members:

                    Segmentation fault (core dumped)

                    Then run it in a debugger and inspect the stack trace (maybe post it here).

                    My feeling is, that you have some old object files that for some reason get linked but are incompatible to the rest of your code.

                    I'd clean all build artifacts and see if it reproduces after a clean build.

                    Regards.

                    Axel SpoerlA Offline
                    Axel SpoerlA Offline
                    Axel Spoerl
                    Moderators
                    wrote on last edited by
                    #11

                    Couldn't resist compiling the reproducer against the latest dev, as well as 6.6 and 6.5.
                    Works and doesn't crash. So I guess @aha_1980 is right about binary artifacts causing the issue.
                    Moving the member function from header to implementation, probably caused no more than a re-compile.

                    Software Engineer
                    The Qt Company, Oslo

                    MesrineM 1 Reply Last reply
                    0
                    • Axel SpoerlA Axel Spoerl

                      Couldn't resist compiling the reproducer against the latest dev, as well as 6.6 and 6.5.
                      Works and doesn't crash. So I guess @aha_1980 is right about binary artifacts causing the issue.
                      Moving the member function from header to implementation, probably caused no more than a re-compile.

                      MesrineM Offline
                      MesrineM Offline
                      Mesrine
                      wrote on last edited by Mesrine
                      #12

                      @Axel-Spoerl

                      Yeah, but if I move it back it gives the runtime error :) .
                      I will try some stack trace and posted here.
                      Also, I will try changing the name of the function as suggested by the remaining binary artifacts idea.

                      1 Reply Last reply
                      0

                      • Login

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