pthread_create valgrind中的内存泄漏

qyswt5oh  于 2023-01-08  发布在  其他
关注(0)|答案(1)|浏览(155)

下面是我的代码的一个简单版本:

#include <pthread.h>
#include <stdio.h>

void* handle_client(void* arg);

int main(int argc, char *argv[])
{
    pthread_t pthr_handle;
    pthread_create(&pthr_handle, NULL, &handle_client, NULL);
    pthread_detach(pthr_handle);
    pthread_exit(NULL);
    return 0;
}

void* handle_client(void* arg)
{
    printf("Hello from thread!\n");
    pthread_exit(NULL);
    return NULL;
}

当我在这个程序上使用valgrind时,它说
可能丢失:1个数据块中272个字节
问题是,并不是每次我运行它的时候它都这么说。有时候它会说没有泄漏。正因为如此,我相信没有泄漏,而且消息与主线程退出后仍在运行的线程有关。pthread_exit调用不是应该让主线程等待其他线程退出吗?我能做些什么让valgrind停止指责内存泄漏吗?

tzdcorbm

tzdcorbm1#

Valgrind无法处理分离线程在主线程之后退出时执行的清理。
运行valgrind时,添加--gen-suppressions=yes标志:

[dbush@db-centos7 ~]$ valgrind --leak-check=full --gen-suppressions=yes ./x1
==18866== Memcheck, a memory error detector
==18866== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==18866== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==18866== Command: ./x1
==18866== 
Hello from thread!
==18866== 
==18866== HEAP SUMMARY:
==18866==     in use at exit: 560 bytes in 1 blocks
==18866==   total heap usage: 6 allocs, 5 frees, 2,192 bytes allocated
==18866== 
==18866== 560 bytes in 1 blocks are possibly lost in loss record 1 of 1
==18866==    at 0x4C2C089: calloc (vg_replace_malloc.c:762)
==18866==    by 0x4012784: _dl_allocate_tls (in /usr/lib64/ld-2.17.so)
==18866==    by 0x4E3F87B: pthread_create@@GLIBC_2.2.5 (in /usr/lib64/libpthread-2.17.so)
==18866==    by 0x400742: main (x1.c:12)
==18866== 
==18866== 
==18866== ---- Print suppression ? --- [Return/N/n/Y/y/C/c] ---- y
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:_dl_allocate_tls
   fun:pthread_create@@GLIBC_2.2.5
   fun:main
}
==18866== LEAK SUMMARY:
==18866==    definitely lost: 0 bytes in 0 blocks
==18866==    indirectly lost: 0 bytes in 0 blocks
==18866==      possibly lost: 560 bytes in 1 blocks
==18866==    still reachable: 0 bytes in 0 blocks
==18866==         suppressed: 0 bytes in 0 blocks
==18866== 
==18866== For lists of detected and suppressed errors, rerun with: -s
==18866== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

在上面的输出中,如果您对打印抑制响应“Y”,则会生成一个抑制记录。您可以将此记录添加到文件中(在显示““的位置添加一个名称<insert_a_suppression_name_here>),然后可以使用--suppressions标志将此文件传递给valgrind:

[dbush@db-centos7 ~]$ valgrind  --suppressions=sup1 ./x1
==18899== Memcheck, a memory error detector
==18899== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==18899== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==18899== Command: ./x1
==18899== 
Hello from thread!
==18899== 
==18899== HEAP SUMMARY:
==18899==     in use at exit: 560 bytes in 1 blocks
==18899==   total heap usage: 6 allocs, 5 frees, 2,192 bytes allocated
==18899== 
==18899== LEAK SUMMARY:
==18899==    definitely lost: 0 bytes in 0 blocks
==18899==    indirectly lost: 0 bytes in 0 blocks
==18899==      possibly lost: 0 bytes in 0 blocks
==18899==    still reachable: 0 bytes in 0 blocks
==18899==         suppressed: 560 bytes in 1 blocks
==18899== 
==18899== For lists of detected and suppressed errors, rerun with: -s
==18899== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

现在它显示为“抑制”泄漏,即您知道并可以安全忽略的泄漏。

相关问题