Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-65756

When QMetaObject::invokeMethod run functor finished, Lambda not been released, memory leak.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • Not Evaluated
    • None
    • 5.10
    • Core: Event loop
    • None

    Description

      Code is simple, used QSharedPointer and deleter to detect the lambda is or not released.

      // Qt lin import
      #include <QtCore>
      #include <QtConcurrent>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QtConcurrent::run( [ ]()
          {
              // Case 1: Memory leak, no "release" log
              forever
              {
                  qDebug( "new" );
                  QSharedPointer< int > test( new int, [ ](int *p){ qDebug( "release" ); delete p; } );
      
                  QMetaObject::invokeMethod( qApp, [ test ]()
                  {
                      qDebug() << test.data();
                  } );
      
                  QThread::sleep( 1 );
              }
      
              // Case 2: Work fine, "release" log has been print;
              forever
              {
                  qDebug( "new" );
                  QSharedPointer< int > test( new int, [ ](int *p){ qDebug( "release" ); delete p; } );
      
                  QtConcurrent::run( [ test ]()
                  {
                      qDebug() << test.data();
                  } );
      
                  QThread::sleep( 1 );
              }
          } );
      
          return a.exec();
      }
      
      

      In both cases, the lambda executes normally.
      but the lambda is not released in case 1, the variables captured into the lambda and the lambda itself not to be released, Cause a memory leak.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              thiago Thiago Macieira
              jasonwong Jason Wong
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes