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

Qt leak thread handles when initiasing QObject in Windows native thread

    XMLWordPrintable

Details

    • 0da841bc337faa00d947cc2e3c273bf87e7cc700

    Description

      Looks like thread handles are leaking when creating and closing Windows native threads. This is issue when developer can't affect to threads handling.

      Test example below prints number of handles before and after creating/closing threads. Looks like number of leaked handles is depending on how fast re-creation/closing happens. If clicking fast (or removing std::cin.get()) number of leaking handles increases massively.

      #include <QObject>
      #include <Windows.h>
      #include <iostream>
      
      DWORD WINAPI ThreadProc(LPVOID)
      {
          QObject o;
      //    FILE f; // doesn't leak
          return 0;
      }
      
      int main(int argc, char *argv[])
      {
          DWORD hndCount(0);
          ::GetProcessHandleCount(::GetCurrentProcess(),&hndCount);
          std::cout << hndCount << std::endl;
          for(size_t i=0; i <10; i++) {
              HANDLE handles[1000];
              for(size_t i=0; i < 1000; ++i)
              {
                  DWORD dwThreadId = 0;
                  handles[i] = ::CreateThread(NULL, 0, &ThreadProc, NULL, 0, &dwThreadId);
              }
              ::GetProcessHandleCount(::GetCurrentProcess(),&hndCount);
              std::cout << hndCount << std::endl << "press to continue";
              std::cin.get();
              for(size_t i=0; i < 1000; ++i) {
                  ::CloseHandle(handles[i]);
              }
              ::GetProcessHandleCount(::GetCurrentProcess(),&hndCount);
              std::cout << hndCount << std::endl;
          }
          return 0;
      }
      
      

      Is there explanation or workaround for the issue?

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            qtcomsupport Qt Support
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes