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
QtWS25 Last Chance

Q_OBJECT memory allocation in a thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
qthread threadsqobjectmemory
13 Posts 5 Posters 1.5k Views
  • 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.
  • C Offline
    C Offline
    chessking5544
    wrote on 15 Dec 2020, 19:32 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Dec 2020, 19:36 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

      C 1 Reply Last reply 15 Dec 2020, 19:37
      0
      • S SGaist
        15 Dec 2020, 19:36

        That depends on how you allocate them.

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 15 Dec 2020, 19:37 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 15 Dec 2020, 19:46 last edited by
          #7

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

          C J 2 Replies Last reply 15 Dec 2020, 19:57
          0
          • C chessking5544
            15 Dec 2020, 19:46

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

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 15 Dec 2020, 19:57 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
              15 Dec 2020, 19:46

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

              J Offline
              J Offline
              JonB
              wrote on 15 Dec 2020, 20:05 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 15 Dec 2020, 20:06 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
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 15 Dec 2020, 20:08 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 15 Dec 2020, 20:21 last edited by
                    #12
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SimonSchroeder
                      wrote on 17 Dec 2020, 08:16 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

                      13/13

                      17 Dec 2020, 08:16

                      • Login

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