Fuite de mémoire avec des pthreads

J’utilise pthreads et, selon Valgrind, je perds de la mémoire, comme dans les erreurs de fuite de mémoire valgrind avec pthread_create.

La réponse du haut indique que si vous connectez tous les threads, cette mémoire sera récupérée, mais ce n’est pas pour moi.

pthread_t threads[NUM_THREADS]; ... for (i = 0; i < NUM_THREADS; i++) { pthread_create(&threads[i], &attr, Worker, NULL); } ... for (i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } 

sortie valgrind

 ==2707== HEAP SUMMARY: ==2707== in use at exit: 954 bytes in 4 blocks ==2707== total heap usage: 7,717 allocs, 7,713 frees, 79,563 bytes allocated ==2707== ==2707== 34 bytes in 1 blocks are still reachable in loss record 1 of 4 ==2707== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==2707== by 0x400541E: local_strdup (dl-load.c:162) ==2707== by 0x40085D3: _dl_map_object (dl-load.c:2473) ==2707== by 0x4012D5C: dl_open_worker (dl-open.c:225) ==2707== by 0x400ECBE: _dl_catch_error (dl-error.c:178) ==2707== by 0x4184D40: do_dlopen (dl-libc.c:89) ==2707== by 0x404CD4B: start_thread (pthread_create.c:308) ==2707== by 0x414BACD: clone (clone.S:130) ==2707== ==2707== 34 bytes in 1 blocks are still reachable in loss record 2 of 4 ==2707== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==2707== by 0x400B05A: _dl_new_object (dl-object.c:161) ==2707== by 0x4006437: _dl_map_object_from_fd (dl-load.c:1051) ==2707== by 0x400839F: _dl_map_object (dl-load.c:2568) ==2707== by 0x4012D5C: dl_open_worker (dl-open.c:225) ==2707== by 0x400ECBE: _dl_catch_error (dl-error.c:178) ==2707== by 0x4184D40: do_dlopen (dl-libc.c:89) ==2707== by 0x404CD4B: start_thread (pthread_create.c:308) ==2707== by 0x414BACD: clone (clone.S:130) ==2707== ==2707== 256 bytes in 1 blocks are still reachable in loss record 3 of 4 ==2707== at 0x402A5E6: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==2707== by 0x401045C: _dl_check_map_versions (dl-version.c:300) ==2707== by 0x401336A: dl_open_worker (dl-open.c:268) ==2707== by 0x400ECBE: _dl_catch_error (dl-error.c:178) ==2707== by 0x4184D40: do_dlopen (dl-libc.c:89) ==2707== by 0x404CD4B: start_thread (pthread_create.c:308) ==2707== by 0x414BACD: clone (clone.S:130) ==2707== ==2707== 630 bytes in 1 blocks are still reachable in loss record 4 of 4 ==2707== at 0x402A5E6: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==2707== by 0x400AE0F: _dl_new_object (dl-object.c:77) ==2707== by 0x4006437: _dl_map_object_from_fd (dl-load.c:1051) ==2707== by 0x400839F: _dl_map_object (dl-load.c:2568) ==2707== by 0x4012D5C: dl_open_worker (dl-open.c:225) ==2707== by 0x400ECBE: _dl_catch_error (dl-error.c:178) ==2707== by 0x4184D40: do_dlopen (dl-libc.c:89) ==2707== by 0x404CD4B: start_thread (pthread_create.c:308) ==2707== by 0x414BACD: clone (clone.S:130) ==2707== ==2707== LEAK SUMMARY: ==2707== definitely lost: 0 bytes in 0 blocks ==2707== indirectly lost: 0 bytes in 0 blocks ==2707== possibly lost: 0 bytes in 0 blocks ==2707== still reachable: 954 bytes in 4 blocks ==2707== suppressed: 0 bytes in 0 blocks ==2707== ==2707== For counts of detected and suppressed errors, rerun with: -v ==2707== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 

Est-ce que je fais quelque chose de mal ou la réponse en haut est-elle incorrecte?

Ce n’est pas une fuite réelle et vous pouvez l’ignorer en toute sécurité. Le bloc de mémoire pthread_create est utilisé pour étendre la stack de threads et n’est pas libéré. La bibliothèque peut utiliser la même région de mémoire pour d’éventuels futurs appels à pthread_create .

Je suis probablement en retard au spectacle. Comme indiqué, il ne doit pas y avoir de fuite de mémoire – j’ai vérifié ce que vous avez fait: c’est un moyen standard de créer (créer de la mémoire interne pour le thread) et de rejoindre un thread (auto récupère cette mémoire).

Cependant, vous n’avez pas montré l’initialisation de attr.

Vous pouvez essayer de faire:

 pthread_create(&threads[i], NULL, Worker, NULL); 

J’ai placé NULL ici, au lieu de toute valeur pour attr. L’initialisation de pthread est étroitement liée à l’atsortingbut (sauf si elle est fournie sous la forme NULL).