Question

I have a multithreaded application in C++ for Windows 7. This app is compiled as a DLL for the use of another C# app.

I run the multithreaded code for long hours to process streaming data. If I compile this C++ code into a stand-alone .exe, and run the code, it works fine.

But when it is turned into a DLL and when the C# code runs with it, the program crashes with Access Violation error after long runs. I see a similar crash when I load the DLL from a Python script and run it for long hours.

When I watch the behavior using Task Manager, I see the System Handle count increasing at a really rapid pace. After around 2 hours of code run, i see the handle count cross 300,000 mark and it keeps increasing.

Inside the multithreaded code, the threads are being created and teared down continuously. I see that after the thread is exited, the thread handle is not closed explicitly by an CloseHandle function. Could this be a potential problem?

How do I confirm that it is a handles issue. What are the ways to watch these system resources in a better way? Before I attempt any fixes, I would like to know all possible solutions as the test runs take a long time, more than 5 hours, to reproduce the crash.

Was it helpful?

Solution

SysInternals Process Explorer can view the handles in a process and identify them as thread, mutex, file, etc. handles. You definitely have to CloseHandle() each thread handle to clean them up.

As an aside, creating a thread is an expensive operation. If you are creating a lot of thread jobs, it is better to queue the jobs and create a pool of a limited number of threads (usually about equal to the number of CPUs in the system) that pull from the queue and perform the job.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top