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. Object instantination and initialization
Forum Updated to NodeBB v4.3 + New Features

Object instantination and initialization

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.6k 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.
  • T Offline
    T Offline
    Tikani
    wrote on 8 Sept 2017, 13:16 last edited by
    #1

    Hi!
    Should object fields be declared as Object obj; or Object* obj; with consequent new in universal initializer list and delete in destructor?
    Why I am asking it - when fields are in the heap memory leaks are a lesser evil on modern PC with 8-16 GB RAM. But when I use Object obj; declaration in a field the risk of stack breakdown appears.

    R 1 Reply Last reply 8 Sept 2017, 13:18
    0
    • T Tikani
      8 Sept 2017, 13:16

      Hi!
      Should object fields be declared as Object obj; or Object* obj; with consequent new in universal initializer list and delete in destructor?
      Why I am asking it - when fields are in the heap memory leaks are a lesser evil on modern PC with 8-16 GB RAM. But when I use Object obj; declaration in a field the risk of stack breakdown appears.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 8 Sept 2017, 13:18 last edited by
      #2

      @Tikani
      here you go.
      Actually this is a basic C++ (beginner) question and not directly related to Qt...

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      T 1 Reply Last reply 8 Sept 2017, 13:26
      1
      • R raven-worx
        8 Sept 2017, 13:18

        @Tikani
        here you go.
        Actually this is a basic C++ (beginner) question and not directly related to Qt...

        T Offline
        T Offline
        Tikani
        wrote on 8 Sept 2017, 13:26 last edited by Tikani 9 Aug 2017, 13:28
        #3

        @raven-worx
        Thanks a lot for the link!
        Sorry for my ignorance, I am C++ newcomer from Java and C#. Because of garrbage collecting in those languages, C++ memory model seems a bit complicated for me and I'm trying to understand it.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 8 Sept 2017, 13:51 last edited by VRonin 9 Aug 2017, 14:52
          #4

          when fields are in the heap memory leaks are a lesser evil on modern PC with 8-16 GB RAM

          Memory leak, seg fault, stack overflow, and race condition are the 4 knights of the apocalypse. They are evil incarnate.


          @Tikani said in Object instantination and initialization:

          But when I use Object obj; declaration in a field the risk of stack breakdown appears.

          Not really, internally Qt allocates almost everything on the heap so for the vast majority of classes sizeof(OneQtClass)<=sizeof(int)+sizeof(void*)


          @Tikani said in Object instantination and initialization:

          and delete in destructor?

          Qt normally uses a parent-child system with the ultimate parent allocated on the stack and its destructor will automatically delete all the children recursively. That's what QObject::setParent and the parent argument to any QObject is for. For QWidgets the parent-child relation is almost obligatory to lay out the visualisation correctly

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          T 1 Reply Last reply 8 Sept 2017, 14:15
          1
          • V VRonin
            8 Sept 2017, 13:51

            when fields are in the heap memory leaks are a lesser evil on modern PC with 8-16 GB RAM

            Memory leak, seg fault, stack overflow, and race condition are the 4 knights of the apocalypse. They are evil incarnate.


            @Tikani said in Object instantination and initialization:

            But when I use Object obj; declaration in a field the risk of stack breakdown appears.

            Not really, internally Qt allocates almost everything on the heap so for the vast majority of classes sizeof(OneQtClass)<=sizeof(int)+sizeof(void*)


            @Tikani said in Object instantination and initialization:

            and delete in destructor?

            Qt normally uses a parent-child system with the ultimate parent allocated on the stack and its destructor will automatically delete all the children recursively. That's what QObject::setParent and the parent argument to any QObject is for. For QWidgets the parent-child relation is almost obligatory to lay out the visualisation correctly

            T Offline
            T Offline
            Tikani
            wrote on 8 Sept 2017, 14:15 last edited by
            #5

            @VRonin
            If I understand you correctly, I should use RAII, i.e. to attempt to use automatic variables everywhere, avoiding the usage of manual dynamic memory management with help of std::unique_ptr, std::shared_ptr, "make"-functions and so on?

            V K 2 Replies Last reply 8 Sept 2017, 14:35
            0
            • T Tikani
              8 Sept 2017, 14:15

              @VRonin
              If I understand you correctly, I should use RAII, i.e. to attempt to use automatic variables everywhere, avoiding the usage of manual dynamic memory management with help of std::unique_ptr, std::shared_ptr, "make"-functions and so on?

              V Offline
              V Offline
              VRonin
              wrote on 8 Sept 2017, 14:35 last edited by
              #6

              @VRonin said in Interfaces, abstract, concrete classes and QObject:

              This is the flowchart I normally follow, it covers 99% of the cases:

              • is it a QObject?
                • yes
                  • can you give it a parent? (for example moveToThread() prevents the parent-child relationship)
                    • yes: use a normal pointer and set the parent
                    • no: connect the deleteLater() slot to some signal
                • no
                  • can multiple places share ownership of the object or pass ownership from one another? (e.g. in containes)
                    • yes: use std::shared_ptr
                    • no: use std::unique_ptr

              If you need to use the pointer in a function that should not manage ownership but just access (read/write) the object (this covers 90% of use cases) then use a raw pointer argument (use std::unique_ptr::get() or std::shared_ptr::get() to retrieve the raw pointer from smart pointers)

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • T Tikani
                8 Sept 2017, 14:15

                @VRonin
                If I understand you correctly, I should use RAII, i.e. to attempt to use automatic variables everywhere, avoiding the usage of manual dynamic memory management with help of std::unique_ptr, std::shared_ptr, "make"-functions and so on?

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 8 Sept 2017, 14:37 last edited by kshegunov 9 Aug 2017, 14:41
                #7

                @Tikani said in Object instantination and initialization:

                If I understand you correctly, I should use RAII, i.e. to attempt to use automatic variables everywhere, avoiding the usage of manual dynamic memory management with help of std::unique_ptr, std::shared_ptr, "make"-functions and so on?

                RAII extends to heap allocations too. It's a concept not a design pattern. The rule of thumb is use whatever's more natural, however esoteric this may sound. For example if you want to keep a QString or a QObject as member, auto-storage's perfectly fine. If on the other hand you want to keep a dynamic list of QObjects then you need to keep them as pointers and most of the time this'd mean you'd heap allocate the objects.

                PS. Because @VRonin raised the question, I advise caution when working with QObjects and shared pointers (std::shared_ptr or QSharedPointer). Shared pointers are okay when an object manages its own lifetime, which for QObject instances is rather rare, so I'd shy away from using them most of the time.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                2

                1/7

                8 Sept 2017, 13:16

                • Login

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