Details
-
Bug
-
Resolution: Done
-
P3: Somewhat important
-
4.8.5, 5.1.0 , 5.2.0 Beta1
-
Windows 7
-
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?