c - How to avoid a memory leak by using pthread_cancel? -
I have a program that should start a thread. To avoid getting out of the software, the thread runs in an endless loop and I am in the thread. This thread never returns the value. So now I have the problem that after detecting the pthread_cancel Valgrind memory leak detection. How can I avoid this kind of memory leak?
Valgrind production:
== 5673 == 1 by 136 blocks by 1 block possibly 8 == 5673 record of 4 necklaces Are lost in == 0x4026A68: calloc (vg_replace_malloc.c: 566) == 5673 by 0x40111FB ==: _dl_allocate_tls (DL- tls.c: 300) == 5673 == 0x404E5A0 by: pthread_create @@GLIBC_2.1 ( Allocatestack.c: 0x804C44E by 580) == 5673 ==: beginning (mythread.c: 25) == 5673 by 0x804D128 ==: main (main.c: 10) code :
int main () {hint (SIGINT, cleanup); Signs (SIGQUIT, cleanliness); Indication (SIGSEGV, cleaning); Start(); Return 0; } Int start () {pthread_create (& thread_id [0], faucet, and threadhandler, faucet); Pthread_join (thread_id [0], zero); Return fault; } Cleanliness () {pthread_cancel (thread_ID [0]); Exit (0); } Zero cleanup_tcp (zero * p) {} zero * Threadhandler (zero * arg) {(zero) arg; Integer = 0; While (TRUE) {pthread_cleanup_push (cleanup_tcp, NULL); Pthread_testcancel (); Fprintf (stderr, "run \ n"); Pthread_cleanup_pop (0); } Return tap; } "itemprop =" text "> You should correctly use pthread_cancel ()
: No resource has been released / regular cleaning of the thread will leak. In your case, it seems that the thread can be allocated to the library, which automatically gets some memory that is not being free. A better approach will be to make a mechanism to notify IMO, thread handler thread which should end it. For example Stable volatile sig_atomic_t done = 0; ... zero cleanup () {done = 1; } Zero * Threadhandler (zero * arg) {while (! Done) fprintf (stderr, "run \ n"); Return tap; }
Comments
Post a Comment