使用pthread_create时出现valgrind内存泄漏错误
valgrind memory leak errors when using pthread_create
我正在使用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
==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_create
时,对于某个函数-我在函数末尾调用函数pthread_exit
。那么,在验证这不是问题之后,问题可能是什么?
线程的资源不会在终止时立即释放,除非创建线程时detach state
属性设置为PTHREAD_CREATE_DETACHED
,或者如果pthread_detach
被调用其CCD_ 7。
未分离的线程将保持终止状态,直到其标识符被传递给pthread_join
或pthread_detach
。
总之,你有三个选择:
- 使用分离的属性集(PTHREAD_create_detached属性(创建线程
- 创建后分离线程(通过调用
pthread_detach
(,或者 - 与终止的线程连接以回收它们(通过调用
pthread_join
(
Hth。
如果线程不应该加入(或者只是自己过期(,可以使线程处于分离状态以避免内存泄漏。
要显式地将线程创建为可连接或已分离,请使用pthread_create((例程中的attr参数。典型的4步流程是:
- 声明
pthread_attr_t
数据类型的pthread属性变量 - 用
pthread_attr_init()
初始化属性变量 - 使用
pthread_attr_setdetachstate()
设置属性分离状态 - 完成后,释放
pthread_attr_destroy()
属性使用的库资源
当不使用可连接线程时,退出线程需要调用pthread_detach(pthread_self())
以便释放其所有资源。
他提到的四个解决方案可以解决这个问题。
然而,如果您的线程是一个长期存在的线程,则可能并不真正需要它。例如,如果pthread贯穿整个过程。
除了其他用户给你的正确答案外,我建议你阅读以下内容:
跟踪多线程C应用程序中的内存泄漏
相关文章:
- 了解 Linux 虚拟内存:valgrind 的 massif 输出显示了有和没有 --pages-as-heap 的主要差异
- Valgrind 在 QThread::start() 上报告内存泄漏
- 使用 valgrind 检查我的链表暗示中的内存泄漏,让我"肯定丢失:1 个块中有 40 个字节"
- Valgrind 在 std::make_unique 中显示内存泄漏
- 为什么 valgrind 报告两个内存分配,而我的代码只请求一个?
- 使用Valgrind时内存泄漏消失
- Valgrind 声称内存释放中的自由空间太多
- 如何通过Valgrind找到QThreads程序中的内存泄漏在哪里?
- 为什么 Valgrind 在此实现中报告内存泄漏?
- 为什么使用 make_unique<> 将函数传递给线程会导致 Valgrind 中的内存泄漏?
- 客户端代码中的 Valgrind 钩子:特定函数的内存泄漏
- 使用 valgrind 的地块测量 c++ 应用程序的最大内存使用量(堆栈和堆)
- Valgrind 错误和内存泄漏与 Python/C API
- Valgrind内存泄漏和错误
- Valgrind C++内存在空文件中泄漏
- 解释Valgrind内存泄漏总结日志
- Valgrind内存泄漏错误的文件跟踪
- 解决valgrind内存错误不指向我的代码
- c++ valgrind内存泄漏,即使内存明显被释放
- 使用pthread_create时出现valgrind内存泄漏错误