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. Q_OBJECT memory allocation in a thread
Forum Updated to NodeBB v4.3 + New Features

Q_OBJECT memory allocation in a thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
qthread threadsqobjectmemory
13 Posts 5 Posters 1.9k Views 3 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
    #3

    @JonB said in Q_OBJECT memory allocation in a thread:

    BTW, what are you intending to do with the QImage in this non-UI thread?

    QImage can be used in a non GUI thread, it's QPixmap which is tied to GUI backend and thus cannot be. See the mandelbrot example for such a use case.

    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
    • C Offline
      C Offline
      chessking5544
      wrote on last edited by
      #4

      Actually, I am using the mandelbrot example as a starting point for my app. I would like to allocate and use a few QImages in the run function, and the question is whether they will be deleted when the thread exits?

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

        That depends on how you allocate them.

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

        Christian EhrlicherC 1 Reply Last reply
        0
        • SGaistS SGaist

          That depends on how you allocate them.

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

          @SGaist said in Q_OBJECT memory allocation in a thread:

          That depends on how you allocate them.

          And is nothing which is specific to Qt btw.

          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 Offline
            C Offline
            chessking5544
            wrote on last edited by
            #7

            how should I allocate QImage so that the memory is freed when the thread is deleted.

            Christian EhrlicherC JonBJ 2 Replies Last reply
            0
            • C chessking5544

              how should I allocate QImage so that the memory is freed when the thread is deleted.

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

              @chessking5544 said in Q_OBJECT memory allocation in a thread:

              how should I allocate QImage so that the memory is freed when the thread is deleted.

              Please don't start with Qt when you don't have basic c++ knowledge.

              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

                how should I allocate QImage so that the memory is freed when the thread is deleted.

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

                @chessking5544
                Sorry, what I meant earlier by

                And what would you do in a destructor? :)

                is that this is a not a new/allocated (pointer) variable. You don't have to destroy it, and what would you write to do so anyway even if you needed to? When the object (your aThread instance) goes out of scope/is destroyed, the QImage/QMutex members will be destroyed without you doing anything.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  chessking5544
                  wrote on last edited by
                  #10

                  well, it seems that regardless of whether I allocate QImage off the stack or the heap, the memory is not freed when the thread exits. that is, an explicit delete must be called. I mention this only because you mentioned that it depends on how you allocate them.

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

                    @chessking5544 said in Q_OBJECT memory allocation in a thread:

                    well, it seems that regardless of whether I allocate QImage off the stack or the heap, the memory is not freed when the thread exits.

                    This has nothing to do with the thread. Since you add it as member of your class the memory is freed when your class gets destroyed. No thread or Qt magic here. Plain c++

                    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
                    1
                    • C Offline
                      C Offline
                      chessking5544
                      wrote on last edited by
                      #12
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SimonSchroeder
                        wrote on last edited by
                        #13

                        As @Christian-Ehrlicher already said: the image gets deleted when the thread object is deleted. Do you delete the thread object?

                        Furthermore, I am not sure how you are trying to check if the memory is actually freed. The operating system might not be able to show you this. Your software will request memory from the OS when necessary, but can choose to keep it for later allocations when memory is freed. The only way you can test this is by repeatedly allocating and deallocating an image. If the memory usage does not grow then memory is correctly freed even if the OS does not show you this.

                        1 Reply Last reply
                        0

                        • Login

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