Valgrind + --leak-check - >不泄漏总结

时间:2017-08-25 11:45:39

标签: c++ valgrind

我有一个多线程程序,它调用一个extern库(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. 如何知道罪魁祸首是我的程序还是lib?
  3. 如下所述,我添加了一个选项以获取更多详细信息。 我收到了很长的报道。 这是一个摘录:

    ==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 个答案:

答案 0 :(得分:0)

  1. 使用valgrind标志--leak-check=full --show-reachable=yes 这将转储所有可访问内存的位置,您需要从那里拿出它来解释谁应该负责处理该内存。

  2. 作为该计划的一个例子:

    #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时,请确保将其取消初始化,如examples

    中所述