Re: pthread memory leaks
On 05/31/11 11:29 AM, jakash3 wrote:
I'm experimenting with signals and conditional variables to implement
thread suspension. The following appears to execute without any
problems but valgrind reports memory leaks.
What can I do to stop the memory leaks? Is there some type of pthread
related memory free function I'm forgetting?
You'd be better of on a Linux or Unix group for this kind of question.
However there are a few C++ specific issues:
=================================
#include<pthread.h>
#include<signal.h>
#include<unistd.h>
#include<cstdio>
#include<cstring>
#include<cstdlib>
pthread_mutex_t m;
pthread_cond_t c;
void freeze(int sig) {
pthread_mutex_lock(&m);
pthread_cond_wait(&c,&m);
pthread_mutex_unlock(&m);
}
void quit(int sig) { pthread_exit(0); }
void* tfunc(void* param) {
while (1) {
puts("hi");
As you have included the C++ versions of the C library headers, fictions
form them like puts should be written std::puts. Some compilers put
them in the namespace, while others (like gcc) declare them in and out
of the namespace, which is a pain.
sleep(1);
}
return NULL;
}
This are all assigned to C function pointers, so they should be declared
as extern "C" (gcc ignores this).
<snip code>
valgrind output:
=================================
==2993==
==2993== HEAP SUMMARY:
==2993== in use at exit: 144 bytes in 1 blocks
==2993== total heap usage: 1 allocs, 0 frees, 144 bytes allocated
==2993==
==2993== LEAK SUMMARY:
==2993== definitely lost: 0 bytes in 0 blocks
==2993== indirectly lost: 0 bytes in 0 blocks
==2993== possibly lost: 144 bytes in 1 blocks
Note this is an in use block, not an actual leak. Probably an internal
I/O buffer.
--
Ian Collins