J
jakash3
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?
=================================
#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");
sleep(1);
}
return NULL;
}
int main() {
pthread_mutex_init(&m, NULL);
pthread_cond_init(&c, NULL);
struct sigaction s;
memset(&s, 0, sizeof(struct sigaction));
/*
* SIGUSR1 signal for thread suspend
* and SIGUSR2 signal for thread exit
*/
s.sa_handler = &freeze;
s.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &s, NULL);
s.sa_handler = &quit;
sigaction(SIGUSR2, &s, NULL);
pthread_t t;
pthread_create(&t, NULL, &tfunc, (void*)NULL);
pthread_detach(t);
getchar();
/* suspend */
pthread_kill(t, SIGUSR1);
getchar();
/* resume */
pthread_cond_signal(&c);
getchar();
/* terminate */
pthread_kill(t, SIGUSR2);
pthread_mutex_destroy(&m);
pthread_cond_destroy(&c);
return 0;
}
=================================
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
==2993== still reachable: 0 bytes in 0 blocks
==2993== suppressed: 0 bytes in 0 blocks
==2993== Rerun with --leak-check=full to see details of leaked memory
==2993==
==2993== For counts of detected and suppressed errors, rerun with: -v
==2993== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from
8)
=================================
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?
=================================
#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");
sleep(1);
}
return NULL;
}
int main() {
pthread_mutex_init(&m, NULL);
pthread_cond_init(&c, NULL);
struct sigaction s;
memset(&s, 0, sizeof(struct sigaction));
/*
* SIGUSR1 signal for thread suspend
* and SIGUSR2 signal for thread exit
*/
s.sa_handler = &freeze;
s.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &s, NULL);
s.sa_handler = &quit;
sigaction(SIGUSR2, &s, NULL);
pthread_t t;
pthread_create(&t, NULL, &tfunc, (void*)NULL);
pthread_detach(t);
getchar();
/* suspend */
pthread_kill(t, SIGUSR1);
getchar();
/* resume */
pthread_cond_signal(&c);
getchar();
/* terminate */
pthread_kill(t, SIGUSR2);
pthread_mutex_destroy(&m);
pthread_cond_destroy(&c);
return 0;
}
=================================
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
==2993== still reachable: 0 bytes in 0 blocks
==2993== suppressed: 0 bytes in 0 blocks
==2993== Rerun with --leak-check=full to see details of leaked memory
==2993==
==2993== For counts of detected and suppressed errors, rerun with: -v
==2993== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from
8)
=================================