为什么调用堆栈数组会导致内存泄漏?

时间:2018-10-28 22:40:09

标签: c++ memory-leaks

我有一个小的代码段,要求用户进行5次加倍操作,运行In [27]: response.css('div.StandardArticleBody_body > p::text').extract() Out[27]: ['SANTIAGO, Oct 26 (Reuters) - Shares in lithium miner SQM jumped 2.7 percent on Friday after Chile’s Constitutional Court rejected a lawsuit to block Chinese miner Tianqi Lithium Corp’s $4.1 billion purchase of a stake in the Chilean lithium miner. ', 'SQM’s B-series shares touched 29,400 pesos ($42.55) at the open of Santiago’s Stock Exchange. '] 时,我发现内存泄漏,说:

valgrind --leak-check=full ./exercise1

我不确定为什么我的这段代码会留下内存。在代码中的任何地方都没有使用堆分配的数组,也没有使用任何指针。这意味着我将不需要使用任何delete []来删除任何对象。我很困惑为什么我还剩下内存。这是我的代码:

==6765==
==6765== HEAP SUMMARY:
==6765==     in use at exit: 72,704 bytes in 1 blocks
==6765==   total heap usage: 8 allocs, 7 frees, 75,037 bytes allocated
==6765==
==6765== LEAK SUMMARY:
==6765==    definitely lost: 0 bytes in 0 blocks
==6765==    indirectly lost: 0 bytes in 0 blocks
==6765==      possibly lost: 0 bytes in 0 blocks
==6765==    still reachable: 72,704 bytes in 1 blocks
==6765==         suppressed: 0 bytes in 0 blocks
==6765== Reachable blocks (those to which a pointer was found) are not shown.
==6765== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==6765==
==6765== For counts of detected and suppressed errors, rerun with: -v
==6765== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

1 个答案:

答案 0 :(得分:3)

对于C ++实现,通常为内部使用分配内存。不释放用于程序全局状态的动态内存也是一种常见的模式。 valgrind报告的“可达”内存与您的代码没有任何关系,因为您的代码未分配任何动态内存。

使用(?!\S)可以告诉您更多信息。我将显示程序的输出

--show-reachable=yes

ld是Linux上的动态链接程序/加载程序库。


P.S。 int main(){} ==10673== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1 ==10673== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==10673== by 0x4E78745: pool (eh_alloc.cc:123) ==10673== by 0x4E78745: __static_initialization_and_destruction_0 (eh_alloc.cc:262) ==10673== by 0x4E78745: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338) ==10673== by 0x40106B9: call_init.part.0 (dl-init.c:72) ==10673== by 0x40107CA: call_init (dl-init.c:30) ==10673== by 0x40107CA: _dl_init (dl-init.c:120) ==10673== by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so) 格式错误,因为double array[size];不是编译时常量表达式。