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. Cannot create QImage using smart pointers
Forum Updated to NodeBB v4.3 + New Features

Cannot create QImage using smart pointers

Scheduled Pinned Locked Moved Unsolved General and Desktop
qimageqpointer
16 Posts 6 Posters 2.7k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #7

    Can you explain exactly how many QImage do you need to generate ?

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

    C 1 Reply Last reply
    0
    • C chessking5544

      @SGaist that makes sense and I should have seen that in the docs. thanks. Will c++ smart pointers work, or is there just no way to do this? I'm looking for a way to get auto deletion for all QImages created.

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

      @chessking5544 said in Cannot create QImage using smart pointers:

      I'm looking for a way to get auto deletion for all QImages created.

      Don't create it on the stack but on the heap. Don't create it on the heap but on the stack. QImage will allocate it's internal image data on the stack anyway.

      /edit: mixed up stack and heap for unknown reasons - fixed :)

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

      JonBJ 1 Reply Last reply
      1
      • SGaistS SGaist

        Can you explain exactly how many QImage do you need to generate ?

        C Offline
        C Offline
        chessking5544
        wrote on last edited by
        #9

        @SGaist most likely a few per thread and maybe a dozen threads. I am coming from the C# world and trying to avoid potential memory leaks by using smart pointers for all dynamic allocation.

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

          As @Christian-Ehrlicher suggests: no need for heap allocation in the case of QImage. That will also free you from having to delete them manually.

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

          C 1 Reply Last reply
          0
          • SGaistS SGaist

            As @Christian-Ehrlicher suggests: no need for heap allocation in the case of QImage. That will also free you from having to delete them manually.

            C Offline
            C Offline
            chessking5544
            wrote on last edited by
            #11

            ok. thanks

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @chessking5544 said in Cannot create QImage using smart pointers:

              I'm looking for a way to get auto deletion for all QImages created.

              Don't create it on the stack but on the heap. Don't create it on the heap but on the stack. QImage will allocate it's internal image data on the stack anyway.

              /edit: mixed up stack and heap for unknown reasons - fixed :)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #12

              @Christian-Ehrlicher said in Cannot create QImage using smart pointers:

              Don't create it on the stack but on the heap.

              Do you want to rephrase this? :)

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #13

                @JonB said in Cannot create QImage using smart pointers:

                Do you want to rephrase this? :)

                What should be rephrased here? It's basic ++ stuff. You can create an object either on the heap with new or directly on the stack.

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

                jsulmJ 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @JonB said in Cannot create QImage using smart pointers:

                  Do you want to rephrase this? :)

                  What should be rephrased here? It's basic ++ stuff. You can create an object either on the heap with new or directly on the stack.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #14

                  @Christian-Ehrlicher "Don't create it on the stack but on the heap." - you should exchange stack and heap :-)

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Christian EhrlicherC 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @Christian-Ehrlicher "Don't create it on the stack but on the heap." - you should exchange stack and heap :-)

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

                    @jsulm Thx, fixed

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

                    1 Reply Last reply
                    0
                    • C chessking5544

                      @SGaist that makes sense and I should have seen that in the docs. thanks. Will c++ smart pointers work, or is there just no way to do this? I'm looking for a way to get auto deletion for all QImages created.

                      S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #16

                      @chessking5544 said in Cannot create QImage using smart pointers:

                      Will c++ smart pointers work, or is there just no way to do this?

                      Just for completeness I will answer this. But first I have to agree with the others that you should allocate QImage on the stack. Good C++ style avoids pointers wherever possible. The best lifetime management is variables on the stack. Containers - and in this case I will count QImage as a container - will internally allocate memory on the heap. This is a good thing as your stack is quite small. I don't know C# well (but assume it has some similarity to Java within this respect), but objects do not need to be allocated using new in C++. If you want polymorphism you can use references instead of pointers. Rarely do you actually need pointers in C++.

                      Now to the answer: C++ smart pointers work with every class. Just use it like this:

                      std::shared_ptr<QImage> image = std::make_shared<QImage>(QSize(1920,1080), QImage::Format_RGB32);
                      

                      Note the use of make_shared here. This will automatically handle out-of-memory errors. Internally it will call new and hand down its parameters to the constructor of QImage. It is advised to use make_shared<QImage>(...) over shared_ptr<QImage>(new QImage(...)) (I can't remember all the reasons).

                      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