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.4k 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.
  • M Mesrine
    24 Jan 2024, 10:15

    @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 Offline
    J Offline
    J.Hilk
    Moderators
    wrote on 24 Jan 2024, 12:12 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.

    M 1 Reply Last reply 24 Jan 2024, 12:21
    0
    • J J.Hilk
      24 Jan 2024, 12:12

      @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")

      M Offline
      M Offline
      Mesrine
      wrote on 24 Jan 2024, 12:21 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.

      K 1 Reply Last reply 24 Jan 2024, 13:04
      0
      • M Mesrine
        24 Jan 2024, 12:21

        @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.

        K Offline
        K Offline
        kkoehne
        Moderators
        wrote on 24 Jan 2024, 13:04 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

        M 1 Reply Last reply 24 Jan 2024, 14:08
        1
        • K kkoehne
          24 Jan 2024, 13:04

          @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

          M Offline
          M Offline
          Mesrine
          wrote on 24 Jan 2024, 14:08 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
          • M Mesrine
            24 Jan 2024, 09:50

            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.

            M Offline
            M Offline
            Mesrine
            wrote on 24 Jan 2024, 14:41 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 A 2 Replies Last reply 24 Jan 2024, 15:06
            0
            • M Mesrine
              24 Jan 2024, 14:41

              @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 Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 24 Jan 2024, 15:06 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.

              M 1 Reply Last reply 24 Jan 2024, 15:21
              0
              • J J.Hilk
                24 Jan 2024, 15:06

                @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 ?

                M Offline
                M Offline
                Mesrine
                wrote on 24 Jan 2024, 15:21 last edited by
                #9

                @J-Hilk yes

                1 Reply Last reply
                0
                • M Mesrine
                  24 Jan 2024, 14:41

                  @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)
                  
                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 24 Jan 2024, 16:33 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.

                  A 1 Reply Last reply 24 Jan 2024, 20:55
                  1
                  • A aha_1980
                    24 Jan 2024, 16:33

                    @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.

                    A Offline
                    A Offline
                    Axel Spoerl
                    Moderators
                    wrote on 24 Jan 2024, 20:55 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

                    M 1 Reply Last reply 24 Jan 2024, 21:52
                    0
                    • A Axel Spoerl
                      24 Jan 2024, 20:55

                      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.

                      M Offline
                      M Offline
                      Mesrine
                      wrote on 24 Jan 2024, 21:52 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

                      12/12

                      24 Jan 2024, 21:52

                      • Login

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