在OSX上,Valgrind报告此内存泄漏,它来自哪里?该代码使用g++作为c++代码进行c编译(我这样做是为了函数重载)。

== 13088 == 1个块中的18个字节肯定在264的丢失记录中丢失了82
== 13088 ==在0x1F25DC:malloc_zone_malloc(vg_replace_malloc.c:267)
== 13088 ==通过0xA1AEDA:malloc_set_zone_name(在/usr/lib/system/libsystem_c.dylib中)
== 13088 ==通过0xA1B4A7:_malloc_initialize(在/usr/lib/system/libsystem_c.dylib中)
== 13088 ==通过0xA1B5DD:malloc_good_size(在/usr/lib/system/libsystem_c.dylib中)
== 13088 ==通过0x4EFA6E:__CFStringChangeSizeMultiple(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x4F3900:CFStringAppend(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x506F91:_convertToURLRepresentation(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x60F963:_CFURLInit(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x4FF268:CFURLCreateWithFileSystemPathRelativeToBase(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x4FF8EE:CFURLCreateWithFileSystemPath(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x515735:_CFBundleGetMainBundleAlreadyLocked(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x515663:CFBundleGetMainBundle(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x539533:cacheBundleInfo(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x5394B3:_CFAppVersionCheckLessThan(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x56C35B:__CFInitialize(在/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation中)
== 13088 ==通过0x8FE11243:ImageLoaderMachO::doImageInit(ImageLoader::LinkContext const&)(在/usr/lib/dyld中)
== 13088 ==通过0x8FE10CB3:ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&)(在/usr/lib/dyld中)
== 13088 ==通过0x8FE0E21F:ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&,unsigned int,ImageLoader::InitializerTimingList&)(在/usr/lib/dyld中)
== 13088 ==通过0x8FE0E1B5:ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&,unsigned int,ImageLoader::InitializerTimingList&)(在/usr/lib/dyld中)
== 13088 ==通过0x8FE0F1BF:ImageLoader::runInitializers(ImageLoader::LinkContext const&,ImageLoader::InitializerTimingList&)(在/usr/lib/dyld中)
== 13088 ==通过0x8FE03655:dyld::initializeMainExecutable()(在/usr/lib/dyld中)
== 13088 ==通过0x8FE07EF1:dyld::_ main(macho_header const *,unsigned long,int,char const **,char const **,char const **)(在/usr/lib/dyld中)
== 13088 ==通过0x8FE012EE:dyldbootstrap::start(macho_header const *,int,char const **,long,macho_header const *)(在/usr/lib/dyld中)
== 13088 ==通过0x8FE01062:_dyld_start(在/usr/lib/dyld中)
== 13088 ==通过0xFFF:???

编辑:另外,我将如何释放此内存?

最佳答案

分配完全不受您控制;对于您来说,免费同样几乎是不可能的。应该将其添加到已知,检测到,记录但被忽略的项目列表中(“被抑制”是行话)。

当我在MacOS X 10.7.2上的valgrind 3.7.0下运行程序时,得到的摘要如下:

==71989==
==71989== HEAP SUMMARY:
==71989==     in use at exit: 6,191 bytes in 33 blocks
==71989==   total heap usage: 33 allocs, 0 frees, 6,191 bytes allocated
==71989==
==71989== LEAK SUMMARY:
==71989==    definitely lost: 0 bytes in 0 blocks
==71989==    indirectly lost: 0 bytes in 0 blocks
==71989==      possibly lost: 0 bytes in 0 blocks
==71989==    still reachable: 6,191 bytes in 33 blocks
==71989==         suppressed: 0 bytes in 0 blocks
==71989== Rerun with --leak-check=full to see details of leaked memory
==71989==
==71989== For counts of detected and suppressed errors, rerun with: -v
==71989== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)

这来自没有显式内存分配的程序-printf()可能会触发一些分配,但是这些字节中的大多数都是在系统库中分配的。您显然已经为回溯(--num-callers=N)设置了比正常值更深的值。

在手册中查找如何正确添加抑制记录,但是valgrind --help提供了:
--num-callers=<number>    show <number> callers in stack traces [12]
--error-limit=no|yes      stop showing new errors if too many? [yes]
--error-exitcode=<number> exit code to return if errors found [0=disable]
--show-below-main=no|yes  continue stack traces below main() [no]
--suppressions=<filename> suppress errors described in <filename>
--gen-suppressions=no|yes|all    print suppressions for errors? [no]

因此,您可以获取valgrind来生成抑制字符串,以将其添加到文件中,然后在后续运行中使用该文件。
Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc

关于c++ - 在OSX上,Valgrind报告此内存泄漏,它来自哪里?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9039251/

10-11 00:05