【问题标题】:Is my valgrind installation broken?我的 valgrind 安装坏了吗?
【发布时间】:2017-08-05 23:04:03
【问题描述】:

我有这个基本的 Hello World 代码:

#include <stdio.h>
int main() {printf("Hello World !"); return 0;}

然后 valgrind 将这个摘要返回给我:

堆摘要:
==17840== 退出时使用:160 个块中的 22,114 个字节
==17840== 总堆使用量:176 次分配,16 次释放,28,258 字节分配
==17840==
==17840== 泄漏摘要:
==17840== 肯定丢失了:63 个块中的 7,960 个字节
==17840== 间接丢失:8 个块中的 6,888 个字节
==17840== 可能丢失:3 个块中的 72 个字节
==17840== 仍然可以访问:1 个块中的 32 个字节
==17840== 抑制:85 个块中的 7,162 个字节

根据 valgrind 的文档,“确定丢失”意味着可修复的泄漏,但是我没有看到任何可修复的泄漏,所以 :
我的 valgrind 坏了吗?

(我使用的是 OSX Sierra 10.12.4)

编辑:绝对是我的 OSX 版本(在我的 Linux 上,我选择“没有泄漏是可能的”),我会尽快更新,完成后我会再次编辑

【问题讨论】:

  • 你使用的是什么版本的 Valgrind?
  • 可以给你用过的valgrind命令吗?
  • valgrind --version 返回:valgrind-3.13.0
  • 我用过valgrind ./a
  • 会不会是因为我修改了内核?

标签: c memory-leaks valgrind


【解决方案1】:

您必须要求获取泄漏信息。我正在开发 macOS Sierra 10.12.6(您应该升级;在 10.12.4 和 10.12.6 之间修复了一些关键的安全问题)。我正在使用从 SVN 构建的 Valgrind(Subversion——尽管它即将改用 Git)。

没有额外的选项

我有一个二叉搜索树程序,我刚刚删除了对内存释放函数的调用,并在我的 valgrind 版本下运行它:

$ valgrind --suppressions=etc/suppressions-macos-10.12.5 -- ./bst29
==5095== Memcheck, a memory error detector
==5095== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5095== Using Valgrind-3.13.0.SVN and LibVEX; rerun with -h for copyright info
==5095== Command: ./bst29
==5095== 
1 3 5 7 10 18 20 
==5095== 
==5095== HEAP SUMMARY:
==5095==     in use at exit: 22,476 bytes in 170 blocks
==5095==   total heap usage: 186 allocs, 16 frees, 28,620 bytes allocated
==5095== 
==5095== LEAK SUMMARY:
==5095==    definitely lost: 24 bytes in 1 blocks
==5095==    indirectly lost: 144 bytes in 6 blocks
==5095==      possibly lost: 0 bytes in 0 blocks
==5095==    still reachable: 0 bytes in 0 blocks
==5095==         suppressed: 22,308 bytes in 163 blocks
==5095== Rerun with --leak-check=full to see details of leaked memory
==5095== 
==5095== For counts of detected and suppressed errors, rerun with: -v
==5095== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 5 from 5)
$

请求--leak-check=full

注意Rerun with --leak-check=full to see details of leaked memory 消息:

$ valgrind --leak-check=full --suppressions=etc/suppressions-macos-10.12.5 -- ./bst29
==5098== Memcheck, a memory error detector
==5098== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5098== Using Valgrind-3.13.0.SVN and LibVEX; rerun with -h for copyright info
==5098== Command: ./bst29
==5098== 
1 3 5 7 10 18 20 
==5098== 
==5098== HEAP SUMMARY:
==5098==     in use at exit: 22,476 bytes in 170 blocks
==5098==   total heap usage: 186 allocs, 16 frees, 28,620 bytes allocated
==5098== 
==5098== 168 (24 direct, 144 indirect) bytes in 1 blocks are definitely lost in loss record 30 of 45
==5098==    at 0x100096861: malloc (vg_replace_malloc.c:302)
==5098==    by 0x100000C50: main (bst29.c:30)
==5098== 
==5098== LEAK SUMMARY:
==5098==    definitely lost: 24 bytes in 1 blocks
==5098==    indirectly lost: 144 bytes in 6 blocks
==5098==      possibly lost: 0 bytes in 0 blocks
==5098==    still reachable: 0 bytes in 0 blocks
==5098==         suppressed: 22,308 bytes in 163 blocks
==5098== 
==5098== For counts of detected and suppressed errors, rerun with: -v
==5098== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 14)
$

修复bst29.c程序后

重新激活树释放代码后,输出为:

$ valgrind --leak-check=full --suppressions=etc/suppressions-macos-10.12.5 -- ./bst29
==5124== Memcheck, a memory error detector
==5124== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5124== Using Valgrind-3.13.0.SVN and LibVEX; rerun with -h for copyright info
==5124== Command: ./bst29
==5124== 
1 3 5 7 10 18 20 
==5124== 
==5124== HEAP SUMMARY:
==5124==     in use at exit: 22,308 bytes in 163 blocks
==5124==   total heap usage: 186 allocs, 23 frees, 28,620 bytes allocated
==5124== 
==5124== LEAK SUMMARY:
==5124==    definitely lost: 0 bytes in 0 blocks
==5124==    indirectly lost: 0 bytes in 0 blocks
==5124==      possibly lost: 0 bytes in 0 blocks
==5124==    still reachable: 0 bytes in 0 blocks
==5124==         suppressed: 22,308 bytes in 163 blocks
==5124== 
==5124== For counts of detected and suppressed errors, rerun with: -v
==5124== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 14 from 14)
$

这对我来说是在 Mac 上的正常结果——所有内容都被隐藏的泄漏摘要。

抑制文件

列出的抑制文件包含各种形式的系统泄漏的 10 个条目:

{
   macOS-Sierra-10.12.5-Leak.1
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc_zone_malloc
   fun:NXCreateMapTableFromZone
   fun:NXCreateMapTableFromZone
   fun:_ZL18__sel_registerNamePKcii
   fun:sel_init
   fun:map_images_nolock
   fun:_ZN11objc_object21sidetable_retainCountEv
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoEbb
   fun:_ZN4dyld21registerObjCNotifiersEPFvjPKPKcPKPK11mach_headerEPFvS1_S6_ESC_
   fun:_dyld_objc_notify_register
   fun:_objc_init
   fun:_os_object_init
}
{
   macOS-Sierra-10.12.5-Leak.2
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc_zone_malloc
   fun:NXCreateHashTableFromZone
   fun:NXCreateHashTable
   fun:NXCreateMapTableFromZone
   fun:NXCreateMapTableFromZone
   fun:_ZL18__sel_registerNamePKcii
   fun:sel_init
   fun:map_images_nolock
   fun:_ZN11objc_object21sidetable_retainCountEv
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoEbb
   fun:_ZN4dyld21registerObjCNotifiersEPFvjPKPKcPKPK11mach_headerEPFvS1_S6_ESC_
   fun:_dyld_objc_notify_register
}
{
   macOS-Sierra-10.12.5-Leak.3
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc_zone_malloc
   fun:NXCreateHashTableFromZone
   fun:NXCreateHashTable
   fun:NXCreateMapTableFromZone
   fun:NXCreateMapTableFromZone
   fun:_ZL18__sel_registerNamePKcii
   fun:sel_init
   fun:map_images_nolock
   fun:_ZN11objc_object21sidetable_retainCountEv
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoEbb
   fun:_ZN4dyld21registerObjCNotifiersEPFvjPKPKcPKPK11mach_headerEPFvS1_S6_ESC_
   fun:_dyld_objc_notify_register
}
{
   macOS-Sierra-10.12.5-Leak.4
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc
   fun:NXCreateHashTableFromZone
   fun:NXCreateHashTable
   fun:NXCreateMapTableFromZone
   fun:NXCreateMapTableFromZone
   fun:_ZL18__sel_registerNamePKcii
   fun:sel_init
   fun:map_images_nolock
   fun:_ZN11objc_object21sidetable_retainCountEv
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoEbb
   fun:_ZN4dyld21registerObjCNotifiersEPFvjPKPKcPKPK11mach_headerEPFvS1_S6_ESC_
   fun:_dyld_objc_notify_register
}
{
   macOS-Sierra-10.12.5-Leak.5
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc
   fun:NXCreateMapTableFromZone
   fun:NXCreateMapTableFromZone
   fun:_ZL18__sel_registerNamePKcii
   fun:sel_init
   fun:map_images_nolock
   fun:_ZN11objc_object21sidetable_retainCountEv
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoEbb
   fun:_ZN4dyld21registerObjCNotifiersEPFvjPKPKcPKPK11mach_headerEPFvS1_S6_ESC_
   fun:_dyld_objc_notify_register
   fun:_objc_init
   fun:_os_object_init
}
{
   macOS-Sierra-10.12.5-Leak.6
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc_zone_calloc
   fun:_NXHashRehashToCapacity
   fun:NXHashInsert
   fun:NXCreateHashTableFromZone
   fun:NXCreateHashTable
   fun:NXCreateMapTableFromZone
   fun:NXCreateMapTableFromZone
   fun:_ZL18__sel_registerNamePKcii
   fun:sel_init
   fun:map_images_nolock
   fun:_ZN11objc_object21sidetable_retainCountEv
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoEbb
}
{
   macOS-Sierra-10.12.5-Leak.7
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:map_images_nolock
   fun:_ZN11objc_object21sidetable_retainCountEv
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoEbb
   fun:_ZN4dyld21registerObjCNotifiersEPFvjPKPKcPKPK11mach_headerEPFvS1_S6_ESC_
   fun:_dyld_objc_notify_register
   fun:_objc_init
   fun:_os_object_init
   fun:libdispatch_init
   fun:libSystem_initializer
   fun:_ZN16ImageLoaderMachO18doModInitFunctionsERKN11ImageLoader11LinkContextE
   fun:_ZN16ImageLoaderMachO16doInitializationERKN11ImageLoader11LinkContextE
}

{
   macOS-Sierra-10.12.5-reachable.1
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc_zone_calloc
   fun:_NXHashRehashToCapacity
   fun:NXHashInsert
   fun:NXCreateHashTableFromZone
   fun:NXCreateHashTable
   fun:NXCreateMapTableFromZone
   fun:NXCreateMapTableFromZone
   fun:_ZL18__sel_registerNamePKcii
   fun:sel_init
   fun:map_images_nolock
   fun:_ZN11objc_object21sidetable_retainCountEv
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoEbb
}

{
   macOS-Sierra-10.12.5-possible.1
   Memcheck:Leak
   match-leak-kinds: possible
   fun:calloc
   fun:map_images_nolock
   fun:_ZN11objc_object21sidetable_retainCountEv
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoEbb
   fun:_ZN4dyld21registerObjCNotifiersEPFvjPKPKcPKPK11mach_headerEPFvS1_S6_ESC_
   fun:_dyld_objc_notify_register
   fun:_objc_init
   fun:_os_object_init
   fun:libdispatch_init
   fun:libSystem_initializer
   fun:_ZN16ImageLoaderMachO18doModInitFunctionsERKN11ImageLoader11LinkContextE
   fun:_ZN16ImageLoaderMachO16doInitializationERKN11ImageLoader11LinkContextE
}

{
    macOS-Sierra-10.12.5-check.1
    Memcheck:Param
    msg->desc.port.name
    fun:mach_msg_trap
    fun:mach_msg
    fun:task_set_special_port
    fun:_os_trace_create_debug_control_port
    fun:_libtrace_init
    fun:libSystem_initializer
    fun:_ZN16ImageLoaderMachO18doModInitFunctionsERKN11ImageLoader11LinkContextE
    fun:_ZN16ImageLoaderMachO16doInitializationERKN11ImageLoader11LinkContextE
    fun:_ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEjPKcRNS_21InitializerTimingListERNS_15UninitedUpwardsE
    fun:_ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEjPKcRNS_21InitializerTimingListERNS_15UninitedUpwardsE
    fun:_ZN11ImageLoader19processInitializersERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
    fun:_ZN11ImageLoader15runInitializersERKNS_11LinkContextERNS_21InitializerTimingListE
}

它们都来自main()被调用之前的启动代码;因此,我对它们中的任何一个都无能为力。

【讨论】:

  • 感谢您的回答,我会尝试升级,但由于我在我的 AMD Hackintosh 上,我需要等待新内核编译完成!
猜你喜欢
  • 2020-08-26
  • 1970-01-01
  • 2012-01-12
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
相关资源
最近更新 更多