DynamoRIO / DynamoRIO/dynamorio
APP CRASH (8.0.18446): Memory leaked after thread creation/deletion on Windows
- Dominant language
- C
- Stars
- 3.2k
- Forks
- 629
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 30
Description
**Describe the bug**
There seems to be a memory leak when running applications that repeatedly create threads (letting them finish each time). As shown by the perfmon graph below, the memory usage continues to increase overtime whereas it stays the same when running natively.
**To Reproduce**
The program is a simple multi-threaded program pasted here:
```
#include
#include
#include
#include
using namespace std;
DWORD WINAPI thread1(LPVOID lpParameter)
{
unsigned int& size = *((unsigned int*)lpParameter);
for (int i = 0; i < 20000000 * (rand()%100); i++)
{
if ((i%100000000 == 0))
{
cout << "thread 1 " << i << endl;
}
}
return 0;
}
DWORD WINAPI thread2(LPVOID lpParameter)
{
unsigned int& size = *((unsigned int*)lpParameter);
for (int i = 0; i < 2000000000; i++)
{
if ((i % 10000000 == 0))
{
cout << "thread 2 " << i << endl;
}
}
return 0;
}
#define NUM 800
int main(int argc, char* argv[])
{
//BOOL debugging = TRUE;
//while (debugging) {
// BOOL debuggerPresent = FALSE;
// if (CheckRemoteDebuggerPresent(GetCurrentProcess(), &debuggerPresent) && debuggerPresent) {
// DebugBreak();
// debugging = FALSE;
// }
// Sleep(1000);
//}
unsigned int s1 = 10;
unsigned int s2 = 20;
DWORD myThreadID[NUM];
HANDLE myHandle[NUM];
while (1)
{
for (int i = 0; i < NUM; i++)
{
myHandle[i] = CreateThread(0, 0, thread1, &s1, 0, myThreadID + i);
}
//cout << myThreadID << " " << myThreadID2 << endl;
for (int i = 0; i < NUM; i++)
{
WaitForSingleObject(myHandle[i], INFINITE);
}
for (int i = 0; i < NUM; i++)
{
cout << CloseHandle(myHandle[i]) << endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(8000));
}
return 0;
}
```
**Versions**
- What version of DynamoRIO are you using? 8.0.18446
- Does the latest build from
https://github.com/DynamoRIO/dynamorio/wiki/Latest-Build solve the problem? No
- What operating system version are you running on? ("Windows 10" is *not* sufficient: give the release number.) Windows 10 OS build 19041.329
- Is your application 32-bit or 64-bit? 32-bit application
**Additional context**
There was a recently filed commit [link here](https://github.com/DynamoRIO/dynamorio/pull/4350) that seemed to address something similar to the issue I am facing here. Though I am not sure it is the same issue and whether i#4334 was tested on Windows. Perhaps the same issue seen in that bug has not yet been address on Windows?
Perfmon graph:

Contributor guide
Research direction
Start with the supplied Windows reproducer, especially its CreateThread, WaitForSingleObject, and CloseHandle loops, and compare memory behavior between native execution and DynamoRIO 8.0.18446 or the latest build. Investigate the related i#4334 and pull request #4350 context mentioned in the report. Done means repeated thread creation and deletion no longer causes memory usage to grow on the stated 32-bit Windows configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- operating-systems, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100