C++ 使用pthread_create时出现valgrind内存泄漏错误
我正在使用pthread库编写一个程序。当我使用命令C++ 使用pthread_create时出现valgrind内存泄漏错误,c++,memory-leaks,pthreads,valgrind,C++,Memory Leaks,Pthreads,Valgrind,我正在使用pthread库编写一个程序。当我使用命令valgrind--leak check=full运行程序时,我得到以下错误描述: ==11784== ==11784== **HEAP SUMMARY:** ==11784== in use at exit: 4,952 bytes in 18 blocks ==11784== total heap usage: 1,059 allocs, 1,041 frees, 51,864 bytes allocated =
valgrind--leak check=full
运行程序时,我得到以下错误描述:
==11784==
==11784== **HEAP SUMMARY:**
==11784== in use at exit: 4,952 bytes in 18 blocks
==11784== total heap usage: 1,059 allocs, 1,041 frees, 51,864 bytes allocated
==11784==
==11784== **288 bytes** in 1 blocks are possibly lost in loss record 2 of 3
==11784== at 0x4C2380C: calloc (vg_replace_malloc.c:467)
==11784== by 0x4010D2E: _dl_allocate_tls (dl-tls.c:300)
==11784== by 0x55DC218: **pthread_create**@@GLIBC_2.2.5 (allocatestack.c:570)
==11784== by 0x401BC0: initdevice(char*) (in /a/fr-01/vol/home/stud/lim/workspace /Ex3/l)
==11784== by 0x406D05: main (in /a/fr-01/vol/home/stud/lim/workspace/Ex3/l)
==11784==
==11784== **4,608 bytes** in 16 blocks are possibly lost in loss record 3 of 3
==11784== at 0x4C2380C: calloc (vg_replace_malloc.c:467)
==11784== by 0x4010D2E: _dl_allocate_tls (dl-tls.c:300)
==11784== by 0x55DC218: **pthread_create**@@GLIBC_2.2.5 (allocatestack.c:570)
==11784== by 0x40268F: write2device(char*, int) (in /a/fr-01/vol/home/stud/lim/workspace/Ex3/l)
==11784== by 0x406D7B: main (in /a/fr-01/vol/home/stud/lim/workspace/Ex3/l)
==11784==
==11784== **LEAK SUMMARY:**
==11784== definitely lost: 0 bytes in 0 blocks
==11784== indirectly lost: 0 bytes in 0 blocks
==11784== possibly lost: 4,896 bytes in 17 blocks
==11784== still reachable: 56 bytes in 1 blocks
==11784== suppressed: 0 bytes in 0 blocks
==11784== Reachable blocks (those to which a pointer was found) are not shown.
==11784== To see them, rerun with: --leak-check=full --show-reachable=yes
==11784==
==11784== For counts of detected and suppressed errors, rerun with: -v
==11784== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)
每次使用某个函数调用
pthread\u create
,我都会调用函数末尾的函数pthread\u exit
。因此,在验证这不是问题之后,什么可能是问题?如果线程不应该被加入(或者只是自己过期),您可以使线程避免内存泄漏
要显式创建可连接或分离的线程,请使用pthread_create()例程中的attr参数。典型的4步流程是:
- 声明
数据类型的pthread属性变量pthread\u attr\u t
- 使用
pthread\u attr\u init()初始化属性变量
- 使用
pthread\u attr\u setdetachstate()
- 完成后,使用
pthread\u attr\u destroy()释放属性使用的库资源
- 线程的资源不会在终止时立即释放,除非
线程是在
分离状态
属性设置为的情况下创建的
PTHREAD\u CREATE\u DETACHED
,或者如果调用了PTHREAD\u detache
它的pthread\u t
未附加的线程将保持终止状态,直到其标识符传递给pthread\u join
或pthread\u detach
总而言之,您有三种选择:
pthread\u Detach
),或pthread\u-Join
)Hth.除了其他用户给您的正确答案外,我建议您阅读以下内容:
当不使用可连接线程时,退出的线程需要调用
pthread\u detach(pthread\u self())
以释放其所有资源。请注意,默认的pthread_创建行为是“可接合”的,而不是分离的。 因此,在pthread完成后,一些操作系统资源仍将保留在进程中,这将导致僵化pthread并导致虚拟/驻留内存使用率增加 他提到的四种解决方案可以解决这个问题 然而,如果您的线程是一个长期存在的线程,那么这可能不是真正需要的。
例如,如果pthread贯穿进程的整个生命周期。那么您是否尝试编写一个只执行pthread_create/pthread_exit(+join)并在valgrind下运行的程序?请尝试使用“-leak check=full--show reachable=yes”来运行它。有人猜测,重复的结构可能不会被删除。但是如果没有代码自己测试它,我就说不出什么了。你是用
pthread\u join
来加入你的线程吗?否则可能会出现泄漏。我对此进行了一点检查,发现当我调用'pthread_detach'而不是pthread_exit'时,不存在我上面提到的内存泄漏问题。可以这样做而不写“pthread_exit”吗?手册页上说:最终应该为创建的每个线程调用pthread_join()或pthread_detach()函数,以便回收与该线程相关的存储。
我已经阅读了该手册页。它仍然没有回答我的问题。啊,对不起:当第一次调用main()的线程以外的线程从用于创建它的启动例程返回时,会隐式调用pthread_exit()。函数的返回值应作为线程的退出状态
您可以让它退出线程分离(pthread_self())为我工作(停止valgrind报告内存泄漏)。这种方法也很方便,因为在动态创建线程时,跟踪所有已启动的线程和执行pthread_join()相当困难。线程可以是可接合的,也可以是分离的。将已分离的线程分离是错误的,因此只能为可接合线程调用它。当“不使用可连接线程”时,顶级函数的简单返回将释放所有资源。请看我的问题。这个答案很有用,但对我没有帮助。