gpt4 book ai didi

C,没有 malloc 的 fork 泄漏

转载 作者:行者123 更新时间:2023-12-05 07:25:53 27 4
gpt4 key购买 nike

我试图了解内存分配如何在 fork 上工作,即使在静态或动态分配上也是如此。我很难理解一些泄漏,如下所示。

有了这个程序:

#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char **argv)
{
pid_t pid;
int status;


pid = fork();
if (pid == 0)
return (0);
else
{
waitpid(pid, &status, 0);
return (0);
}
}

我正在使用 valgrind 获取此日志文件:

==81268== Memcheck, a memory error detector
==81268== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==81268== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==81268== Command: ./a.out
==81268== Parent PID: 73556
==81268==
==81268== Syscall param msg->desc.port.name points to uninitialised byte(s)
==81268== at 0x1003A834A: mach_msg_trap (in /usr/lib/system/libsystem_kernel.dylib)
==81268== by 0x1003A7796: mach_msg (in /usr/lib/system/libsystem_kernel.dylib)
==81268== by 0x1003A1485: task_set_special_port (in /usr/lib/system/libsystem_kernel.dylib)
==81268== by 0x10053D10E: _os_trace_create_debug_control_port (in /usr/lib/system/libsystem_trace.dylib)
==81268== by 0x10053D458: _libtrace_init (in /usr/lib/system/libsystem_trace.dylib)
==81268== by 0x1000A69DF: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==81268== by 0x100018A1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==81268== by 0x100018C1D: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==81268== by 0x1000144A9: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==81268== by 0x100014440: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==81268== by 0x100013523: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==81268== by 0x1000135B8: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==81268== Address 0x10488bc6c is on thread 1's stack
==81268== in frame #2, created by task_set_special_port (???:)
==81268==
==81269==
==81269== HEAP SUMMARY:
==81269== in use at exit: 28,557 bytes in 172 blocks
==81269== total heap usage: 188 allocs, 16 frees, 34,701 bytes allocated
==81269==
==81269== 72 bytes in 3 blocks are possibly lost in loss record 31 of 55
==81269== at 0x100098BF2: calloc (in /Users/simrossi/.brew/Cellar/valgrind/3.13.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==81269== by 0x1005B1846: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==81269== by 0x1005C4FE8: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==81269== by 0x10000803B: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==81269== by 0x100008255: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==81269== by 0x1001FF00A: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==81269== by 0x1005B1074: _objc_init (in /usr/lib/libobjc.A.dylib)
==81269== by 0x10019268D: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==81269== by 0x10019263A: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==81269== by 0x1000A69D5: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==81269== by 0x100018A1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==81269== by 0x100018C1D: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==81269==
==81269== 2,064 (16 direct, 2,048 indirect) bytes in 1 blocks are definitely lost in loss record 51 of 55
==81269== at 0x100098586: malloc (in /Users/simrossi/.brew/Cellar/valgrind/3.13.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==81269== by 0x1004E9103: _nc_table_new (in /usr/lib/system/libsystem_notify.dylib)
==81269== by 0x1004E4FB5: _notify_init_globals (in /usr/lib/system/libsystem_notify.dylib)
==81269== by 0x1004F2AC0: _os_once (in /usr/lib/system/libsystem_platform.dylib)
==81269== by 0x1004F2A91: _os_alloc_once (in /usr/lib/system/libsystem_platform.dylib)
==81269== by 0x1004E50AD: _notify_fork_child (in /usr/lib/system/libsystem_notify.dylib)
==81269== by 0x1000A6B20: libSystem_atfork_child (in /usr/lib/libSystem.B.dylib)
==81269== by 0x10026F436: fork (in /usr/lib/system/libsystem_c.dylib)
==81269== by 0x100000F4A: main (in ./a.out)
==81269==
==81269== 2,064 (16 direct, 2,048 indirect) bytes in 1 blocks are definitely lost in loss record 52 of 55
==81269== at 0x100098586: malloc (in /Users/simrossi/.brew/Cellar/valgrind/3.13.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==81269== by 0x1004E9103: _nc_table_new (in /usr/lib/system/libsystem_notify.dylib)
==81269== by 0x1004E4FC6: _notify_init_globals (in /usr/lib/system/libsystem_notify.dylib)
==81269== by 0x1004F2AC0: _os_once (in /usr/lib/system/libsystem_platform.dylib)
==81269== by 0x1004F2A91: _os_alloc_once (in /usr/lib/system/libsystem_platform.dylib)
==81269== by 0x1004E50AD: _notify_fork_child (in /usr/lib/system/libsystem_notify.dylib)
==81269== by 0x1000A6B20: libSystem_atfork_child (in /usr/lib/libSystem.B.dylib)
==81269== by 0x10026F436: fork (in /usr/lib/system/libsystem_c.dylib)
==81269== by 0x100000F4A: main (in ./a.out)
==81269==
==81269== LEAK SUMMARY:
==81269== definitely lost: 32 bytes in 2 blocks
==81269== indirectly lost: 4,096 bytes in 2 blocks
==81269== possibly lost: 72 bytes in 3 blocks
==81269== still reachable: 200 bytes in 6 blocks
==81269== suppressed: 24,157 bytes in 159 blocks
==81269== Reachable blocks (those to which a pointer was found) are not shown.
==81269== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==81269==
==81269== For counts of detected and suppressed errors, rerun with: -v
==81269== Use --track-origins=yes to see where uninitialised values come from
==81269== ERROR SUMMARY: 5 errors from 4 contexts (suppressed: 13 from 13)
==81268==
==81268== HEAP SUMMARY:
==81268== in use at exit: 18,127 bytes in 159 blocks
==81268== total heap usage: 175 allocs, 16 frees, 24,271 bytes allocated
==81268==
==81268== 72 bytes in 3 blocks are possibly lost in loss record 26 of 42
==81268== at 0x100098BF2: calloc (in /Users/simrossi/.brew/Cellar/valgrind/3.13.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==81268== by 0x1005B1846: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==81268== by 0x1005C4FE8: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==81268== by 0x10000803B: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==81268== by 0x100008255: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==81268== by 0x1001FF00A: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==81268== by 0x1005B1074: _objc_init (in /usr/lib/libobjc.A.dylib)
==81268== by 0x10019268D: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==81268== by 0x10019263A: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==81268== by 0x1000A69D5: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==81268== by 0x100018A1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==81268== by 0x100018C1D: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==81268==
==81268== LEAK SUMMARY:
==81268== definitely lost: 0 bytes in 0 blocks
==81268== indirectly lost: 0 bytes in 0 blocks
==81268== possibly lost: 72 bytes in 3 blocks
==81268== still reachable: 200 bytes in 6 blocks
==81268== suppressed: 17,855 bytes in 150 blocks
==81268== Reachable blocks (those to which a pointer was found) are not shown.
==81268== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==81268==
==81268== For counts of detected and suppressed errors, rerun with: -v
==81268== Use --track-origins=yes to see where uninitialised values come from
==81268== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 12 from 12)

我不明白泄漏的那些 32 字节和 4096 字节是从哪里来的?它来自标准库吗,我在 stackoverflow 上看到了类似的东西。我在 Mac OS Sierra x86_64 上。

编辑:如果我不 fork ,我不会有任何泄漏,它似乎与函数调用有关。

最佳答案

macOS 上程序的启动代码分配了大量从未释放的内存。对于正在分配的内存,您无能为力。但是,您可以将泄漏告诉 Valgrind 并说服它忽略它们。我相信 fork() 是无关紧要的 — 即使没有它,您也可能会看到泄漏。

您需要运行这样的程序,并带有生成抑制的选项,在文件中捕获抑制,然后(将来)运行指定您创建的抑制文件。

您可以在 Valgrind shows memory leak with empty main without including headers 看到一个有效的例子.

关于C,没有 malloc 的 fork 泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54671093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com