Memory Leaks when multi-threading

Memory Leaks when multi-threading

Post by QmVuIE1lbm » Sat, 28 Oct 2006 13:38:02


Hi,

I have a C++ win32 service project i'm working on (non-MFC).

It currenly enumerates all the active processes and runs a CheckProcess
function against each process. If the process is not allowed by our company
it kills it.

My functions are leak free, I tested many iterations. However I want it to
fire CheckProcess in a new thread, so that it catches things faster. So I
used _createthreadex to fire up CheckProcess. So now every iteration fires
up around ~40 threads (depends on how many processes are running) to check
all at once. However, when I do that, memory leaks occur... I do call
CloseHandle on the returned handle.

any ideas what I could be missing just based on this ? Otherwise I can try
to compile some sample to demonstrate my issue.

Thank you so much for any advice!
Ben
 
 
 

Memory Leaks when multi-threading

Post by anton bass » Sat, 28 Oct 2006 18:19:36

Ben,

I am afraid your approach is basically wrong....


You should clearly realize that, *IN ACTUALITY*, all your treads just
cannot run simultaneously - the maximal *THEORETICALLY* possible number
of threads that may
run at the same time equals that of CPUs on the target machine.

Therefore, by taking the above approach, you waste A LOT of memory
(each thread has its own stack, plus don't forget about ETHREAD
structures that has to be allocated for every thread), plus make thread
dispatcher waste time on unnecessary context switches.


In other words, by creating 40 threads you degrade, rather than
improve, your performance, and waste the system resources.....

Anton Bassov
.

 
 
 

Memory Leaks when multi-threading

Post by QmVuIE1lbm » Sat, 28 Oct 2006 22:13:02

I even tried limiting the threads to one at a time. Still leaks occured.

I wasn't using the /Md, but I switched to that after reading some posts
about leaks when linking to CRT statically... so switched to this model, but
it didn't help at all.

Any other suggestions ? Thanks !
 
 
 

Memory Leaks when multi-threading

Post by QmVuIE1lbm » Sun, 29 Oct 2006 01:24:02

Cool, I found the issue. During my troubleshooting I added a critical
section to my thread class to protect against starting two threads at once.
I failed to call DeleteCriticalSection in the destructor. Now with this in
place, and the /Md compiler option, the leaks stopped.

Any tips on finding these things easier ? when this was happening i was
doing an object dump using _CrtMemDumpAllObjectsSince and it didn't catch
that undeleted critical_section.

Thank you,
Ben
 
 
 

Memory Leaks when multi-threading

Post by QmVuIE1lbm » Sun, 29 Oct 2006 04:30:02

k, next issue... this solved my leaks. However when I tried to run the
service on a different PC it gives that 14001 SxS error... from my reading
it's since the target PC doesn't have the C runtime and it would need to be
installed. Man... that sucks... is there no way to link to the static lib
using /MT and avoid memory leaks ?!? Does MFC thread creation have the same
isssue ? maybe I can switch to MFC and link statically to those libs.

Thanks for any advice !

"Ben Menashe" wrote:

 
 
 

Memory Leaks when multi-threading

Post by Carl Danie » Sun, 29 Oct 2006 11:57:58


Yes, just build with /MT. you've wrwongly convinced yourself that you need
to us /MD(d)

-cd
 
 
 

Memory Leaks when multi-threading

Post by Vladimir S » Sun, 29 Oct 2006 17:25:00

Anton is right.

Good approach is to design systems that does not depend linearly upon the
amount of tasks to process. Instead make a couple of working threads that
process the tasks. But actually in your case thread pool is also usless.
Making one thread that enumerates and kills (by the way you should realize
that during enumeration the process might be killed and process this issue
in your CheckProcess routine) is enough: on an average machine there're up
to 100 of processes.

--
Vladimir
 
 
 

Memory Leaks when multi-threading

Post by Vladimir S » Sun, 29 Oct 2006 17:28:57


[...]

http://www.yqcomputer.com/

--
Vladimir