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. Memory leak in a QThread

Memory leak in a QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 2.2k Views 1 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.
  • S Offline
    S Offline
    skylendar
    wrote on last edited by
    #1

    Hi there and thx for reading and answering this post if you can.

    I'm dealing with a memory leak in a slot executed by a QThread connected as a Qt::QueuedConnection.

    Of course, I've tried -fsanitize=thread, then -sanitize=address without success. Either the program crashes before it runs the thread (whereas it runs fine without sanitize) or I get pointless returns.

    Do you have an idea about how to deal with such an issue ?

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

      Hi,

      Maybe share the slot code incriminated ?

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

      S 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        Maybe share the slot code incriminated ?

        S Offline
        S Offline
        skylendar
        wrote on last edited by
        #3

        @SGaist

        Ok I could but it is long, complex and the thread crashes at different places, but this is not the point.
        In such a case, how to isolate in this case the memory leak with tools like sanitize or valgrind. I have of course already tried but got disappointing results.

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

          What make you think it's a leak ?
          Are you sure you're not accessing an invalid pointer ?
          Did you check your allocations/deallocations ?
          Are you getting data from another library ?
          If so, who owns that data and thus who's responsible for freeing it ?

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

          S 1 Reply Last reply
          4
          • S skylendar

            @SGaist

            Ok I could but it is long, complex and the thread crashes at different places, but this is not the point.
            In such a case, how to isolate in this case the memory leak with tools like sanitize or valgrind. I have of course already tried but got disappointing results.

            S Offline
            S Offline
            skylendar
            wrote on last edited by
            #5

            @skylendar

            Another problem is that when I run my program with one of those tools, the program may run flawlessly, and without it crashes...

            VRoninV 1 Reply Last reply
            0
            • SGaistS SGaist

              What make you think it's a leak ?
              Are you sure you're not accessing an invalid pointer ?
              Did you check your allocations/deallocations ?
              Are you getting data from another library ?
              If so, who owns that data and thus who's responsible for freeing it ?

              S Offline
              S Offline
              skylendar
              wrote on last edited by
              #6

              @SGaist

              good questions, when my app crashes, I get: free(): invalid next size (normal): 0x00007f5f7006fff0 ***
              Abandon (core dumped)
              .
              my ptr is correctly allocated, but something pollutes the malloc structure so that it can't free cleanly.

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

                You're using C style memory allocation ?

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

                S 1 Reply Last reply
                0
                • SGaistS SGaist

                  You're using C style memory allocation ?

                  S Offline
                  S Offline
                  skylendar
                  wrote on last edited by
                  #8

                  @SGaist

                  no, a classical ptr = new myclass.

                  jsulmJ 1 Reply Last reply
                  0
                  • S skylendar

                    @SGaist

                    no, a classical ptr = new myclass.

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

                    @skylendar Before suspecting QThread of having a memory leak you should post your code here or at least crash dump, else people here can only guess...

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

                    1 Reply Last reply
                    2
                    • S skylendar

                      @skylendar

                      Another problem is that when I run my program with one of those tools, the program may run flawlessly, and without it crashes...

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      @skylendar said in Memory leak in a QThread:

                      Another problem is that when I run my program with one of those tools, the program may run flawlessly, and without it crashes

                      This clearly points to a race condition.

                      in a slot executed by a QThread connected as a Qt::QueuedConnection

                      If you are subclassing QThread and adding a slot you are falling in one of the most common misconceptions about QThread: it is not the thread, it's a wrapper around it. Slots of the QThread will be executed by the event loop that created the thread, not the one spawned by the thread. See https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                      "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
                      4
                      • BuckwheatB Offline
                        BuckwheatB Offline
                        Buckwheat
                        wrote on last edited by
                        #11

                        @jsulm said in Memory leak in a QThread:

                        uspecting QThread of having a memory leak you should post your code here or at least crash dump, else people here can only guess

                        Hi @skylendar
                        Yes, it would be nice to see your code a little. One trick I have started using when passing pointers across signals is to use QSharedPointer.

                        typedef QSharedPointer <Type> PType; // Also works with QSharedDataPointer and QExplicitlySharedDataPointer

                        slot would be:
                        void doSomething (const PType& value);

                        calling conv:
                        PType type (new Type);
                        Q_EMIT doSomething (type);

                        This allows for cleanup and a little safety on the pointer since it is reference counted. You can pass it along and know it will get there. If the event queue ends, the copy is destroyed. When the slot is finished, the copy is destroyed (if reference is 0). When the caller exits, the pointer is destroyed if the reference is 0. There is a little more overhead than a straight pointer, but you do not have to worry about who is the current owner of the pointer or synchronize a return. For me, it is useful in callbacks.

                        You will, however, have to register PType so the signal/slot mechanism works happily. I use with both the old SIGNAL()/SLOT() and new method of function pointers equally well.

                        typedef QSharedPointer <LogInfo> PLogInfo;

                        ...

                        qRegisterMetaType <PLogInfo> ("PLogInfo");

                        Experiment. For my app, I use a lot of QSharedData and I have not seen a huge performance hit but I only run 20Hz and 50Hz sensor data. But no memory leaks from threading or messaging.

                        Dave Fileccia

                        1 Reply Last reply
                        1

                        • Login

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