JSO*_*SON 1 c++ boost valgrind memory-leaks
我正在尝试增强线程,并且从valgrind中注意到,它只是通过循环一个空的代码块而泄漏了320个字节。我从2010年开始在google上发现了一些帖子,表明它们很可能是虚假肯定,因为在valgind运行之前未关闭线程,但这有点不同。在这些示例中,您仍有一些仍可访问的块(因此,如果线程仍在运行,则可释放),其中我的运行显示8个仍可访问,而20个已确定丢失。这是我应该担心的事情,还是我不知所措?谢谢
代码
#include <boost/thread.hpp>
#include <iostream>
#define THREADS 20
void threadfunc(int workerid) {}
int main(int argc, char **argv){
boost::thread *threads[THREADS];
int i;
for (i = 0; i < THREADS; i++) {
threads[i] = new boost::thread(threadfunc, i);
}
for (i = 0; i < THREADS; i++) {
threads[i]->join();
}
}
Run Code Online (Sandbox Code Playgroud)
编译命令
c++ -o example example.cpp -I /usr/include/boost -lboost_system -lboost_thread
Run Code Online (Sandbox Code Playgroud)
预设命令
G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=40 --log-file=valgrind.log ./example
Run Code Online (Sandbox Code Playgroud)
精氨酸结果
==31674== HEAP SUMMARY:
==31674== in use at exit: 328 bytes in 21 blocks
==31674== total heap usage: 103 allocs, 82 frees, 14,968 bytes allocated
==31674==
==31674== Searching for pointers to 21 not-freed blocks
==31674== Checked 215,920 bytes
==31674==
==31674== 8 bytes in 1 blocks are still reachable in loss record 1 of 2
==31674== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31674== by 0x4E454A9: boost::detail::get_once_per_thread_epoch() (in /usr/lib/libboost_thread.so.1.46.1)
==31674== by 0x4E3E4FF: ??? (in /usr/lib/libboost_thread.so.1.46.1)
==31674== by 0x4E3E7C8: boost::detail::get_current_thread_data() (in /usr/lib/libboost_thread.so.1.46.1)
==31674== by 0x4E3FF3A: boost::thread::join() (in /usr/lib/libboost_thread.so.1.46.1)
==31674== by 0x402C79: main (in /home/Jason/php/base/example)
==31674==
==31674== 320 bytes in 20 blocks are definitely lost in loss record 2 of 2
==31674== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31674== by 0x402C2A: main (in /home/Jason/php/base/example)
==31674==
==31674== LEAK SUMMARY:
==31674== definitely lost: 320 bytes in 20 blocks
==31674== indirectly lost: 0 bytes in 0 blocks
==31674== possibly lost: 0 bytes in 0 blocks
==31674== still reachable: 8 bytes in 1 blocks
==31674== suppressed: 0 bytes in 0 blocks
==31674==
==31674== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
--31674--
--31674-- used_suppression: 2 dl-hack3-cond-1
==31674==
==31674== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
Run Code Online (Sandbox Code Playgroud)
这是您的错误,而不是boost::threads。您的记忆没有被释放。
for (i = 0; i < THREADS; i++) {
threads[i] = new boost::thread(threadfunc, i);
}
Run Code Online (Sandbox Code Playgroud)
从主功能退出之前,您必须释放内存(删除线程)。就像是
for (i = 0; i < THREADS; i++) {
delete threads[i];
}
Run Code Online (Sandbox Code Playgroud)
或加入后删除下一个。
| 归档时间: |
|
| 查看次数: |
2053 次 |
| 最近记录: |