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. There is an smart and safe pointer to a QString object?
Forum Updated to NodeBB v4.3 + New Features

There is an smart and safe pointer to a QString object?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 6 Posters 1.6k Views 4 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.
  • G Offline
    G Offline
    giusdbg
    wrote on last edited by
    #1

    Hi.

    I am trying to share a variable QString between a origin class and a destination class.
    So if the variable in an object is changed, the change is also seen in the other object.

    dest class
    
    QString *refString;
    
    void setRefString (QString &refStringIn)
    {
          refString = &refStringIn;
    }
    
    
    usage
    
    destObj->setRefString (origString)
    

    But this for the various automatic mechanisms present in the QT is very unsafe, for example this becomes access outside the memory because the QString object is destroyed after }

    void unsafeFunc
    {
         QString unsafeString = "xyz"
    
         destObj->setRefString (unsafeString)
    }
    

    Is there a way of avoiding it?
    Without having any impact on the origin QString object, for example only by increasing and decreasing the number of references on the origin QString object.

    Christian EhrlicherC 1 Reply Last reply
    0
    • G giusdbg

      Hi.

      I am trying to share a variable QString between a origin class and a destination class.
      So if the variable in an object is changed, the change is also seen in the other object.

      dest class
      
      QString *refString;
      
      void setRefString (QString &refStringIn)
      {
            refString = &refStringIn;
      }
      
      
      usage
      
      destObj->setRefString (origString)
      

      But this for the various automatic mechanisms present in the QT is very unsafe, for example this becomes access outside the memory because the QString object is destroyed after }

      void unsafeFunc
      {
           QString unsafeString = "xyz"
      
           destObj->setRefString (unsafeString)
      }
      

      Is there a way of avoiding it?
      Without having any impact on the origin QString object, for example only by increasing and decreasing the number of references on the origin QString object.

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

      Why do someone want to store a QString in a pointer in the first place? It's not needed...

      Use std::shared_ptr<> if you need a smart pointer...

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

      G 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Why do someone want to store a QString in a pointer in the first place? It's not needed...

        Use std::shared_ptr<> if you need a smart pointer...

        G Offline
        G Offline
        giusdbg
        wrote on last edited by
        #3

        @Christian-Ehrlicher Because I would like to smart and safe handle various types of cases.
        For example the QString object might come from a class that I can't modify, and that QString object shouldn't be destroyed just because the dest object is destroyed.

        I'm aware that I'm perhaps beyond good programming in QT, but I need it in some complex and convoluted cases, and to learn what you can and shouldn't do with all the automatisms of object management that QT applies.

        Christian EhrlicherC 1 Reply Last reply
        0
        • G giusdbg

          @Christian-Ehrlicher Because I would like to smart and safe handle various types of cases.
          For example the QString object might come from a class that I can't modify, and that QString object shouldn't be destroyed just because the dest object is destroyed.

          I'm aware that I'm perhaps beyond good programming in QT, but I need it in some complex and convoluted cases, and to learn what you can and shouldn't do with all the automatisms of object management that QT applies.

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

          @giusdbg said in There is an smart and safe pointer to a QString object?:

          For example the QString object might come from a class that I can't modify, and that QString object shouldn't be destroyed just because the dest object is destroyed.

          Why should this be needed? It's a string...

          What you can and shouldn't do with all the automatisms of object management that QT applies.

          I don't see what this has to do with Qt at all. a QString is nothing which should be created on the heap.

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

          G 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @giusdbg said in There is an smart and safe pointer to a QString object?:

            For example the QString object might come from a class that I can't modify, and that QString object shouldn't be destroyed just because the dest object is destroyed.

            Why should this be needed? It's a string...

            What you can and shouldn't do with all the automatisms of object management that QT applies.

            I don't see what this has to do with Qt at all. a QString is nothing which should be created on the heap.

            G Offline
            G Offline
            giusdbg
            wrote on last edited by
            #5

            @Christian-Ehrlicher QTs have very interesting and useful mechanisms, which are applied automatically in many objects, implicit sharing, copy-on-write, shallow copies, etc. .

            This obviously (or not obviously, probably depends on how much time you spent debugging strange behaviors due to anomalous use of the QT) makes the type of use I would like to do more complicated.

            Christian EhrlicherC 1 Reply Last reply
            0
            • G giusdbg

              @Christian-Ehrlicher QTs have very interesting and useful mechanisms, which are applied automatically in many objects, implicit sharing, copy-on-write, shallow copies, etc. .

              This obviously (or not obviously, probably depends on how much time you spent debugging strange behaviors due to anomalous use of the QT) makes the type of use I would like to do more complicated.

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

              If you want help then please describe your usecase properly - so noone can help you.
              A QString on the heap is not really a good idea nor needed in most of the use cases so ...

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

              G 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                If you want help then please describe your usecase properly - so noone can help you.
                A QString on the heap is not really a good idea nor needed in most of the use cases so ...

                G Offline
                G Offline
                giusdbg
                wrote on last edited by giusdbg
                #7

                @Christian-Ehrlicher
                For the moment I'm mainly trying to understand the QT environment (doing an update of a program from QT3 to QT4), it's the first time I using a system where smart pointers are so intensely and widely used.

                Sometimes some triks can save days of work, and I'm trying to figure out what I can and shouldn't do using QTs, even crashing into them.

                From what I understand this my anomalous use of the QString is to be avoided, too many problems that cannot be managed decently.

                Christian EhrlicherC W 2 Replies Last reply
                0
                • G giusdbg

                  @Christian-Ehrlicher
                  For the moment I'm mainly trying to understand the QT environment (doing an update of a program from QT3 to QT4), it's the first time I using a system where smart pointers are so intensely and widely used.

                  Sometimes some triks can save days of work, and I'm trying to figure out what I can and shouldn't do using QTs, even crashing into them.

                  From what I understand this my anomalous use of the QString is to be avoided, too many problems that cannot be managed decently.

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

                  @giusdbg said in There is an smart and safe pointer to a QString object?:

                  From what I understand this my anomalous use of the QString is to be avoided, too many problems that cannot be managed decently.

                  No, there are no problems with that - it's simply not needed and useless to wrap a QString into a (smart)pointer.

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

                  G 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @giusdbg said in There is an smart and safe pointer to a QString object?:

                    From what I understand this my anomalous use of the QString is to be avoided, too many problems that cannot be managed decently.

                    No, there are no problems with that - it's simply not needed and useless to wrap a QString into a (smart)pointer.

                    G Offline
                    G Offline
                    giusdbg
                    wrote on last edited by giusdbg
                    #9

                    @Christian-Ehrlicher smart pointer is a good indirect suggestion.

                    QPointer<QString> is a great option for various cases.
                    P.S. WRONG, QPointer work only with QObject based object.

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • G giusdbg has marked this topic as solved on
                    • G giusdbg

                      @Christian-Ehrlicher smart pointer is a good indirect suggestion.

                      QPointer<QString> is a great option for various cases.
                      P.S. WRONG, QPointer work only with QObject based object.

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

                      @giusdbg said in There is an smart and safe pointer to a QString object?:

                      QPointer<QString> is a great option for various cases.

                      No, for sure not...

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

                      G 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        @giusdbg said in There is an smart and safe pointer to a QString object?:

                        QPointer<QString> is a great option for various cases.

                        No, for sure not...

                        G Offline
                        G Offline
                        giusdbg
                        wrote on last edited by giusdbg
                        #11

                        @Christian-Ehrlicher True, QPointer work only with QObject based object, interesting solution but not for my case.

                        Case closed in any case (dead end).

                        1 Reply Last reply
                        0
                        • G giusdbg

                          @Christian-Ehrlicher
                          For the moment I'm mainly trying to understand the QT environment (doing an update of a program from QT3 to QT4), it's the first time I using a system where smart pointers are so intensely and widely used.

                          Sometimes some triks can save days of work, and I'm trying to figure out what I can and shouldn't do using QTs, even crashing into them.

                          From what I understand this my anomalous use of the QString is to be avoided, too many problems that cannot be managed decently.

                          W Offline
                          W Offline
                          wrosecrans
                          wrote on last edited by
                          #12

                          @giusdbg said in There is an smart and safe pointer to a QString object?:

                          For the moment I'm mainly trying to understand the QT environment (doing an update of a program from QT3 to QT4), it's the first time I using a system where smart pointers are so intensely and widely used.

                          It's a bit confusing what about Qt you are trying to understand here. It seems like there is some sort of "XY problem" underlying your question. https://xyproblem.info/

                          Everything you are asking about would be the same with an int or any other data type in C++. If you keep a raw pointer to something that might be destroyed elsewhere, you'll have problems. If you want to change the state of an object, you should use the public interface of that object to do it, and not try to poke directly at pointers to members of that object. Normally, you would just call some setText() function or something like that.

                          If you explain what you are actually trying to accomplish, people might be able to suggest some approach, but it won't be this.

                          G 1 Reply Last reply
                          1
                          • W wrosecrans

                            @giusdbg said in There is an smart and safe pointer to a QString object?:

                            For the moment I'm mainly trying to understand the QT environment (doing an update of a program from QT3 to QT4), it's the first time I using a system where smart pointers are so intensely and widely used.

                            It's a bit confusing what about Qt you are trying to understand here. It seems like there is some sort of "XY problem" underlying your question. https://xyproblem.info/

                            Everything you are asking about would be the same with an int or any other data type in C++. If you keep a raw pointer to something that might be destroyed elsewhere, you'll have problems. If you want to change the state of an object, you should use the public interface of that object to do it, and not try to poke directly at pointers to members of that object. Normally, you would just call some setText() function or something like that.

                            If you explain what you are actually trying to accomplish, people might be able to suggest some approach, but it won't be this.

                            G Offline
                            G Offline
                            giusdbg
                            wrote on last edited by giusdbg
                            #13

                            @wrosecrans Yes, thanks for your reply.

                            The code that I had to fix taking a few days of work, in QT3 worked perfectly, in QT4 it caused access to destroyed objects, use of uninitialized variables, etc., without crashing the program, just writing junk to a config file.

                            It's not the QT's fault, it was just caused by the way they functioned and being used beyond their limit.

                            I tried to explain that I'm not looking for a solution to a problem, but only to analyze, to extremes, a particular case that I had to face and see if there was some programming technique, a trick, that I'm not able to see, to explore the limits of QT and above all the limits of my knowledge.

                            From my point of view the correct answer to my question is

                            • It cannot be done, it will always remain a dangerous use.
                            • There would be QPointer which would solve the problem, but it only works with QObject based objects.
                            JonBJ SGaistS 2 Replies Last reply
                            0
                            • G giusdbg

                              @wrosecrans Yes, thanks for your reply.

                              The code that I had to fix taking a few days of work, in QT3 worked perfectly, in QT4 it caused access to destroyed objects, use of uninitialized variables, etc., without crashing the program, just writing junk to a config file.

                              It's not the QT's fault, it was just caused by the way they functioned and being used beyond their limit.

                              I tried to explain that I'm not looking for a solution to a problem, but only to analyze, to extremes, a particular case that I had to face and see if there was some programming technique, a trick, that I'm not able to see, to explore the limits of QT and above all the limits of my knowledge.

                              From my point of view the correct answer to my question is

                              • It cannot be done, it will always remain a dangerous use.
                              • There would be QPointer which would solve the problem, but it only works with QObject based objects.
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #14

                              @giusdbg said in There is an smart and safe pointer to a QString object?:

                              There would be QPointer which would solve the problem, but it only works with QObject based objects.

                              I think this is the key to what you were looking for (whatever the existing code behind might be doing). QPointer does just what (I think) you want, but it only works for QObjects, and the very base Qt classes (like QString or QList) are "lightweight" and not QObjects. Indeed, the ability of QPointer to work as it does relies on how QObjects/QMetaObjects work.

                              By the way, did you see/for your interest Qt also has QScopedPointer Class? For the record that would have worked with, say, QString *, it's not a QObject thing. But I suspect (a) it doesn't do enough of what you want, it's a very specific scope thing and (b) would mean changing other code, don't think you want to do that.

                              G 1 Reply Last reply
                              1
                              • G giusdbg

                                @wrosecrans Yes, thanks for your reply.

                                The code that I had to fix taking a few days of work, in QT3 worked perfectly, in QT4 it caused access to destroyed objects, use of uninitialized variables, etc., without crashing the program, just writing junk to a config file.

                                It's not the QT's fault, it was just caused by the way they functioned and being used beyond their limit.

                                I tried to explain that I'm not looking for a solution to a problem, but only to analyze, to extremes, a particular case that I had to face and see if there was some programming technique, a trick, that I'm not able to see, to explore the limits of QT and above all the limits of my knowledge.

                                From my point of view the correct answer to my question is

                                • It cannot be done, it will always remain a dangerous use.
                                • There would be QPointer which would solve the problem, but it only works with QObject based objects.
                                SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @giusdbg said in There is an smart and safe pointer to a QString object?:

                                @wrosecrans Yes, thanks for your reply.

                                The code that I had to fix taking a few days of work, in QT3 worked perfectly, in QT4 it caused access to destroyed objects, use of uninitialized variables, etc., without crashing the program, just writing junk to a config file.

                                It's not the QT's fault, it was just caused by the way they functioned and being used beyond their limit.

                                I tried to explain that I'm not looking for a solution to a problem, but only to analyze, to extremes, a particular case that I had to face and see if there was some programming technique, a trick, that I'm not able to see, to explore the limits of QT and above all the limits of my knowledge.

                                From my point of view the correct answer to my question is

                                • It cannot be done, it will always remain a dangerous use.
                                • There would be QPointer which would solve the problem, but it only works with QObject based objects.

                                Third option: it needs cleanup or porting or both.

                                You may have discovered issues that were hidden. A proper fix will be way better than any workaround.

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

                                G 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @giusdbg said in There is an smart and safe pointer to a QString object?:

                                  There would be QPointer which would solve the problem, but it only works with QObject based objects.

                                  I think this is the key to what you were looking for (whatever the existing code behind might be doing). QPointer does just what (I think) you want, but it only works for QObjects, and the very base Qt classes (like QString or QList) are "lightweight" and not QObjects. Indeed, the ability of QPointer to work as it does relies on how QObjects/QMetaObjects work.

                                  By the way, did you see/for your interest Qt also has QScopedPointer Class? For the record that would have worked with, say, QString *, it's not a QObject thing. But I suspect (a) it doesn't do enough of what you want, it's a very specific scope thing and (b) would mean changing other code, don't think you want to do that.

                                  G Offline
                                  G Offline
                                  giusdbg
                                  wrote on last edited by
                                  #16

                                  @JonB Yes, your answer is a rather complete analysis of the situation, both the concrete one of the problem, and the attempt to investigate alternative programming techniques and tricks.

                                  In the end I decided that if I have to spend days of work, I prefer to remove, improve and adapt the code to the QT (e.g. removes all non-strictly local uses of char * , .toAscii() , .data(), etc.) rather than adding other possible layers of complexity and probable malfunction.

                                  P.S. Personally, I found all the replies and discussions on Qt Forum useful, at least they pushed me to see beyond my knowledge limits, and to try new things, which were useful to me shortly after (or as soon as my knowledge was sufficient to use them).

                                  Thanks for the various help.

                                  This is the currently bug free (touch wood 🙃) program

                                  Screenshot_20230624_150517.png

                                  1 Reply Last reply
                                  1
                                  • SGaistS SGaist

                                    @giusdbg said in There is an smart and safe pointer to a QString object?:

                                    @wrosecrans Yes, thanks for your reply.

                                    The code that I had to fix taking a few days of work, in QT3 worked perfectly, in QT4 it caused access to destroyed objects, use of uninitialized variables, etc., without crashing the program, just writing junk to a config file.

                                    It's not the QT's fault, it was just caused by the way they functioned and being used beyond their limit.

                                    I tried to explain that I'm not looking for a solution to a problem, but only to analyze, to extremes, a particular case that I had to face and see if there was some programming technique, a trick, that I'm not able to see, to explore the limits of QT and above all the limits of my knowledge.

                                    From my point of view the correct answer to my question is

                                    • It cannot be done, it will always remain a dangerous use.
                                    • There would be QPointer which would solve the problem, but it only works with QObject based objects.

                                    Third option: it needs cleanup or porting or both.

                                    You may have discovered issues that were hidden. A proper fix will be way better than any workaround.

                                    G Offline
                                    G Offline
                                    giusdbg
                                    wrote on last edited by giusdbg
                                    #17

                                    @SGaist Yes, this is also true, various problems were due to code that should not be written even in QT 3.

                                    The cleanup and reorganization of the code has been done where it would have been thoughtless not to do it, and in preparation for the jump to QT 5/6 .

                                    But there are so many problems to deal with, tons of things to learn almost from scratch, so I analyze the situation of the various things, and assign the priority at what and how to deal with them.

                                    And so for me it is very useful to try to learn new techniques, tricks, to cut edges, so that I can deal with problems based on their importance, usefulness and current knowledge, and postpone the rest.

                                    SGaistS 1 Reply Last reply
                                    0
                                    • G giusdbg

                                      @SGaist Yes, this is also true, various problems were due to code that should not be written even in QT 3.

                                      The cleanup and reorganization of the code has been done where it would have been thoughtless not to do it, and in preparation for the jump to QT 5/6 .

                                      But there are so many problems to deal with, tons of things to learn almost from scratch, so I analyze the situation of the various things, and assign the priority at what and how to deal with them.

                                      And so for me it is very useful to try to learn new techniques, tricks, to cut edges, so that I can deal with problems based on their importance, usefulness and current knowledge, and postpone the rest.

                                      SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @giusdbg cutting edges by using global pointers is usually the wrong idea. Even if it might look counter intuitive, taking the time to work on the design now will save you time later on.

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

                                      1 Reply Last reply
                                      1
                                      • Kent-DorfmanK Offline
                                        Kent-DorfmanK Offline
                                        Kent-Dorfman
                                        wrote on last edited by
                                        #19

                                        Sometimes you need to throw out the baby with the bath water.

                                        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