我正在开发一个C++应用程序,该应用程序可以读取输入内容并对其进行操作
引擎,将信息发送到另一台设备。
主程序保持无限循环,每次重复相同的过程
5秒。
我正在用Valgrind分析上述应用程序,它显示了以下结果:
==4453== 120 bytes in 1 blocks are still reachable in loss record 6 of 8
. . .
==4453== 106 byt4es in 1 blocks are possibly lost in loss record 7 of 8
. . .
==4453== LEAK SUMMARY:
==4453== definitely lost: 0 bytes in 0 blocks
==4453== indirectly lost: 0 bytes in 0 blocks
==4453== possibly lost: 148 bytes in 2 blocks
==4453== still reachable: 2,310 bytes in 8 blocks
==4453== suppressed: 0 bytes in 0 blocks
如果我让程序在无限循环中执行的迭代次数增加,则“可能丢失”类别中的字节数保持不变,但是“仍然可达”类别中的字节数与迭代次数成比例地增加执行(每次程序重复循环中包含的指令,仍可访问的字节数增加)。我对此有以下疑问:
最佳答案
是的,“仍然可以到达”的字节是内存泄漏。
如果程序持续运行,最终系统将耗尽内存并崩溃。
应该解决。
由于最终崩溃,应解决该问题。
另外,在非常复杂的程序中,人们只是放弃了,他们安装了监视程序或计时器,每小时仅重新启动一次程序。这很愚蠢,但有时愚蠢的解决方案是最便宜,最快的解决方案。便宜,快速,好:选两个。
解决这个问题应该是微不足道的。
我编写了一个内存泄漏程序并运行了它。从顶部向下看几行,上面写着operator new[]
然后是main (leak-test.cpp:7)
。 Valgrind会为您精确分配。
$ valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all ./leak-test
==929671==
==929671== HEAP SUMMARY:
==929671== in use at exit: 8,000 bytes in 1 blocks
==929671== total heap usage: 3 allocs, 2 frees, 81,728 bytes allocated
==929671==
==929671== 8,000 bytes in 1 blocks are still reachable in loss record 1 of 1
==929671== at 0x4095504: operator new[](unsigned long) (vg_replace_malloc.c:431)
==929671== by 0x100009F7: main (leak-test.cpp:7)
==929671==
==929671== LEAK SUMMARY:
==929671== definitely lost: 0 bytes in 0 blocks
==929671== indirectly lost: 0 bytes in 0 blocks
==929671== possibly lost: 0 bytes in 0 blocks
==929671== still reachable: 8,000 bytes in 1 blocks
==929671== suppressed: 0 bytes in 0 blocks
==929671==
==929671== For lists of detected and suppressed errors, rerun with: -s
==929671== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
关于c++ - Valgrind:是否可以接受越来越多的可能丢失的字节?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62536372/