gpt4 book ai didi

c - pthread_created 后跟 pthread_join valgrind 可能损失 c

转载 作者:行者123 更新时间:2023-12-03 05:01:29 25 4
gpt4 key购买 nike

我遇到了有关丢失内存的 valgrind 错误问题。这是我的代码:

if((err = pthread_create(&handlert, NULL, &handler, NULL)) != 0) perror(..)

if((err = pthread_create(&mastert , NULL, &createmaster, NULL)) != 0) perror(..)

for(int i = 0; i < THREADSINPOOL; i++) {
if((err = pthread_create(&(f[i]), NULL, &createpool, NULL)) != 0) perror(..)
}

if((err = pthread_join(handlert,(void*) &sRet[1])) != 0) perror(..)

if((err = pthread_join(mastert,(void*) &lRet[1])) != 0) perror(..)

for(int i = 0; i < THREADSINPOOL; i++) {
if((err = pthread_join(f[i], (void*) &wRet[i])) != 0) perror(..)
}
return 0;

我为每个线程都有一个连接,我正在检查结果,但 valgrind 仍然说:

==21610== 560 bytes in 1 blocks are possibly lost in loss record 8 of 12
==21610== at 0x4C2CC90: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21610== by 0x4012E44: allocate_dtv (dl-tls.c:296)
==21610== by 0x4012E44: _dl_allocate_tls (dl-tls.c:460)
==21610== by 0x4E3FCC0: allocate_stack (allocatestack.c:589)
==21610== by 0x4E3FCC0: pthread_create@@GLIBC_2.2.5 (pthread_create.c:495)
==21610== by 0x401B3B: main (myfile.c:85)

(每个 pthread 创建时都会出现相同的错误,仅更改代码行)预先感谢您!

编辑:编译中的标志: --leak-check=full -std=c99 -Wall -pedantic -g -DMAKE_VALGRIND_HAPPY只有这个错误(看起来不像之前错误的结果)

最佳答案

我认为这不是真正的泄漏,或者即使是,也很可能发生在低级代码中。如果它应该被视为泄漏还是急切的优化,这是有争议的。

参见http://sourceware.org/ml/glibc-bugs/2007-04/msg00036.html :

It is not a real leak. As far as i know The buffer allocated at pthread_create() is used to extend the stack of threads. If you pthread_join() and pthread_create() again The old position in the stack will be used by the new one.

https://stackoverflow.com/a/17642468/714501 中也提到了这一点

some implementations of POSIX threads (I'm guessing you're using glibc/NPTL) cache and reuse thread resources rather than freeing them fully.

我认为对于这种情况你可以安装 valgrind 抑制:

http://valgrind.org/docs/manual/mc-manual.html#mc-manual.suppfiles

关于c - pthread_created 后跟 pthread_join valgrind 可能损失 c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54435972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com