How to check individual threads for memory leaks
-
My work often involves writing multi-threaded turnkey systems - ie. programs which are designed to run "for ever". As such the kind of memory leaks that are found by BoundsChecker - memory that is allocated but not released when the program terminates - are relatively unimportant. What does cause problems is when a thread, running as a continuous loop, allocates memory each time round the loop that it doesn't free. Over time the system floods until eventually a catastrophic failure occurs. I need a way to monitor the heap usage of each individual thread, and detect whether it is leaking each time it executes its loop. (I know there is something called "thread-local storage" but I haven't been able to find much detail on it). If anyone knows any techniques for this, or can point me at some good articles or books, I'd be most grateful. Dave
-
My work often involves writing multi-threaded turnkey systems - ie. programs which are designed to run "for ever". As such the kind of memory leaks that are found by BoundsChecker - memory that is allocated but not released when the program terminates - are relatively unimportant. What does cause problems is when a thread, running as a continuous loop, allocates memory each time round the loop that it doesn't free. Over time the system floods until eventually a catastrophic failure occurs. I need a way to monitor the heap usage of each individual thread, and detect whether it is leaking each time it executes its loop. (I know there is something called "thread-local storage" but I haven't been able to find much detail on it). If anyone knows any techniques for this, or can point me at some good articles or books, I'd be most grateful. Dave
I had the same problems a year back and ended up writing a sort of collector where I registered all dynamically allocated objects (and general storage) an unregistered them on deletion. The collector was instanciated on the stack for each thread. When the thread exited, the collector printed information about all objects still not unregistered. If there is a better way to do this, I also would like to know.
-
My work often involves writing multi-threaded turnkey systems - ie. programs which are designed to run "for ever". As such the kind of memory leaks that are found by BoundsChecker - memory that is allocated but not released when the program terminates - are relatively unimportant. What does cause problems is when a thread, running as a continuous loop, allocates memory each time round the loop that it doesn't free. Over time the system floods until eventually a catastrophic failure occurs. I need a way to monitor the heap usage of each individual thread, and detect whether it is leaking each time it executes its loop. (I know there is something called "thread-local storage" but I haven't been able to find much detail on it). If anyone knows any techniques for this, or can point me at some good articles or books, I'd be most grateful. Dave
As the name implies TLS is synonym of thread specific data. Use TlsAlloc on process/dll initialization, this will allocate a TLS index, then for each thread that needs specific instance data allocate some storage and pass the pointer to TlsSetValue, associating the TLS index with it. When a thread needs to get the instance value use TlsGetValue. Finally when all your threads are over, use TlsFree to release the Tls Index. The nº of TLS index is limited by process to 64 on 95 ??? , 64 or 80 in 98/me and 10?? in XP, please check msdn for the correct value, I don't remember clearly You could circumvent this if you wish by providing a pointer to a list/map of pseudo TLS indexes. Joao Vaz Unhappy TCL programmer