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. Need an advice with pointer
Qt 6.11 is out! See what's new in the release blog

Need an advice with pointer

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 739 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.
  • sitesvS Offline
    sitesvS Offline
    sitesv
    wrote on last edited by sitesv
    #1

    Hi! I typed a short simple code... Is there a good way to avoid memory leak in case of 'return'?
    I don't want to 'delete mstr' in each 'return' statement.

    void my_func(){
             my_struct *mstr = new my_struct;
             if(!check1()) return;
             mstr->val1 = val1;
             if(!check2()) return;
             mstr->val2 = val2;
             my_vector.push_bach(mstr);
    }
    
    KroMignonK 1 Reply Last reply
    0
    • sitesvS sitesv

      Hi! I typed a short simple code... Is there a good way to avoid memory leak in case of 'return'?
      I don't want to 'delete mstr' in each 'return' statement.

      void my_func(){
               my_struct *mstr = new my_struct;
               if(!check1()) return;
               mstr->val1 = val1;
               if(!check2()) return;
               mstr->val2 = val2;
               my_vector.push_bach(mstr);
      }
      
      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #2

      @sitesv said in Need an advice with pointer:

      I don't want to 'delete mstr' in each 'return' statement.

      use smart pointers:

      • c++ standards
      • Qt smart pointers

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      sitesvS 1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Or do both checks before and return early since if any of the two fails you do not add anything to your vector.

        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
        3
        • KroMignonK KroMignon

          @sitesv said in Need an advice with pointer:

          I don't want to 'delete mstr' in each 'return' statement.

          use smart pointers:

          • c++ standards
          • Qt smart pointers
          sitesvS Offline
          sitesvS Offline
          sitesv
          wrote on last edited by
          #4

          @KroMignon said in Need an advice with pointer:

          use smart pointers:

          But how to be if pointer was added to the vector? (in case of smart pointers)

          KroMignonK 1 Reply Last reply
          0
          • sitesvS sitesv

            @KroMignon said in Need an advice with pointer:

            use smart pointers:

            But how to be if pointer was added to the vector? (in case of smart pointers)

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #5

            @sitesv said in Need an advice with pointer:

            But how to be if pointer was added to the vector? (in case of smart pointers)

            You could use std::unique_ptr<>() or Qt equivalent QScopedPointer<>():

            void my_func(){
                     auto mstr = std::unique_ptr<my_struct>(new my_struct);
                     //auto mstr = QScopedPointer<my_struct>(new my_struct);
                     if(!check1()) return;
                     mstr->val1 = val1;
                     if(!check2()) return;
                     mstr->val2 = val2;
                     // convert smart pointer back to raw pointer
                     my_vector.push_back(mstr.release());
                     //=> for QScopedPointer my_vector.push_back(mstr.take());
            }
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            sitesvS 1 Reply Last reply
            3
            • KroMignonK KroMignon

              @sitesv said in Need an advice with pointer:

              But how to be if pointer was added to the vector? (in case of smart pointers)

              You could use std::unique_ptr<>() or Qt equivalent QScopedPointer<>():

              void my_func(){
                       auto mstr = std::unique_ptr<my_struct>(new my_struct);
                       //auto mstr = QScopedPointer<my_struct>(new my_struct);
                       if(!check1()) return;
                       mstr->val1 = val1;
                       if(!check2()) return;
                       mstr->val2 = val2;
                       // convert smart pointer back to raw pointer
                       my_vector.push_back(mstr.release());
                       //=> for QScopedPointer my_vector.push_back(mstr.take());
              }
              
              sitesvS Offline
              sitesvS Offline
              sitesv
              wrote on last edited by
              #6

              @KroMignon
              Big thanks to you!

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                A version without useless memory allocation:

                void my_func()
                {
                    if (!check1() || !check2()) {
                        return;
                    }
                    my_struct *mstr = new my_struct;
                    mstr->val1 = val1;
                    mstr->val2 = val2;
                    my_vector.push_back(mstr);
                }
                

                It can even be shorter if my_struct has a constructor.

                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
                2

                • Login

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