瓦尔格林德 + --泄漏检查 -> 不泄漏摘要

Valgrind + --leak-check -> not leak summary

本文关键字:泄漏 gt 检查 林德      更新时间:2023-10-16

我有一个多线程程序,它调用一个外部库(log4cplus(。
当我记录某些内容时,内存消耗会增加,但永远不会减少。

我用 --leak-check=yes选项运行了 valgrind。我得到 :

HEAP SUMMARY
==413==     in use at exit: 2,944,909 bytes in 23,429 blocks
==413==   total heap usage: 99,472,873 allocs, 99,449,444 frees, 8,049,350,322 bytes allocated

但是我没有收到任何泄漏内存摘要。

  1. 这是否意味着 2,944,909 字节仍然可以访问,因此在停止进程时被释放?
  2. 如何知道罪魁祸首是我的程序还是库?

如下所述,我添加了一个选项来获取更多详细信息。 我有一份很长的报告。 这是摘录:

==18326== 6,480 bytes in 15 blocks are still reachable in loss record 3,959 of 4,015
==18326==    at 0x482B728: operator new(unsigned int) (vg_replace_malloc.c:328)
==18326==    by 0x15F04F8: __gnu_cxx::new_allocator<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent>::allocate(unsigned int, void const*) (new_allocator.h:104)
==18326==    by 0x15EFB7F: std::allocator_traits<std::allocator<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent> >::allocate(std::allocator<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent>&, unsigned int) (alloc_traits.h:416)
==18326==    by 0x15EF320: std::_Deque_base<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent, std::allocator<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent> >::_M_allocate_node() (stl_deque.h:600)
==18326==    by 0x15EE678: std::_Deque_base<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent, std::allocator<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent> >::_M_create_nodes(Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent**, Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent**) (stl_deque.h:725)
==18326==    by 0x15ED5AF: std::_Deque_base<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent, std::allocator<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent> >::_M_initialize_map(unsigned int) (stl_deque.h:699)
==18326==    by 0x15EBFEF: std::_Deque_base<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent, std::allocator<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent> >::_Deque_base() (stl_deque.h:490)
==18326==    by 0x15EB045: std::deque<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent, std::allocator<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent> >::deque() (stl_deque.h:884)
==18326==    by 0x15ECF6C: Koin::Common::Containers::StdQueueWrapper<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent, true>::clear() (StdQueueWrapper.h:57)
==18326==    by 0x15EB880: Koin::Common::Containers::StdQueueWrapper<Koin::Common::Logging::Log4Cplus::InternalLoggingWrapperEvent, true>::pop_front() (StdQueueWrapper.h:36)
==18326==    by 0x15E9D62: Koin::Common::Logging::Log4Cplus::Log4CplusLoggerProcessor::Execute() (Log4CplusLoggerProcessor.cpp:63)
==18326==    by 0x15F1617: void std::__invoke_impl<void, void (Koin::Common::Logging::Log4Cplus::Log4CplusLoggerProcessor::* const&)(), Koin::Common::Logging::Log4Cplus::Log4CplusLoggerProcessor*>(std::__invoke_memfun_deref, void (Koin::Common::Logging::Log4Cplus::Log4CplusLoggerProcessor::* const&)(), Koin::Common::Logging::Log4Cplus::Log4CplusLoggerProcess  or*&&) (functional:235)
  1. 使用瓦尔格林德旗帜--leak-check=full --show-reachable=yes这将转储所有可访问内存的位置,您需要从那里获取它来推理谁应该负责处置该内存。

作为此程序的示例:

#include <stdlib.h>
char *var;
void myfunc(char *c)
{
var = c;
}
int main(int argc, char *argv[])
{
myfunc(malloc(4));
return 0;
}
$ valgrind   --leak-check=full --show-reachable=yes ./a.out 
==21480== Memcheck, a memory error detector
==21480== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==21480== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==21480== Command: ./a.out
==21480== 
==21480== 
==21480== HEAP SUMMARY:
==21480==     in use at exit: 4 bytes in 1 blocks
==21480==   total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==21480== 
==21480== 4 bytes in 1 blocks are still reachable in loss record 1 of 1
==21480==    at 0x4A06A2E: malloc (vg_replace_malloc.c:270)
==21480==    by 0x4004DC: main (t.c:6)
==21480== 
==21480== LEAK SUMMARY:
==21480==    definitely lost: 0 bytes in 0 blocks
==21480==    indirectly lost: 0 bytes in 0 blocks
==21480==      possibly lost: 0 bytes in 0 blocks
==21480==    still reachable: 4 bytes in 1 blocks
==21480==         suppressed: 0 bytes in 0 blocks
==21480== 
==21480== For counts of detected and suppressed errors, rerun with: -v
==21480== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 8 from 6)

在这里,valgrind 将向您展示 main(( 中的第 11 行已经分配了 4 字节内存,您需要通过 myfunc(( 等方式跟踪分配的内存的最终位置,以及关于谁应该释放此内存的原因。

当您使用 log4cplus 时,请确保取消初始化它,如示例中所述