KIY*_*IYZ 4 c debugging macos memory-leaks command-line-tool
leaks命令行工具将报告
像下面这样:
\nProcess: checker [84357]\nPath: /path/to/program\nLoad Address: 0x104703000\nIdentifier: checker\nVersion: ???\nCode Type: X86-64\nParent Process: zsh [64610]\n\nDate/Time: 2019-11-30 18:43:06.864 -0800\nLaunch Time: 2019-11-30 18:42:58.593 -0800\nOS Version: Mac OS X 10.13.4 (17E199)\nReport Version: 7\nAnalysis Tool: /usr/bin/leaks\n\nPhysical footprint: 300K\nPhysical footprint (peak): 300K\n----\n\nleaks Report Version: 3.0\nProcess 84357: 161 nodes malloced for 17 KB\nProcess 84357: 3 leaks for 64 total leaked bytes.\n\nLeak: 0x7fdf5b400350 size=16 zone: DefaultMallocZone_0x10470e000\nLeak: 0x7fdf5b4027c0 size=16 zone: DefaultMallocZone_0x10470e000\nLeak: 0x7fdf5b402810 size=32 zone: DefaultMallocZone_0x10470e000\nRun Code Online (Sandbox Code Playgroud)\n我的问题是,如何使用这些信息来实际跟踪并查找源代码中哪些 malloc 调用没有\xe2\x80\x99t 对应的free()调用?
\n如何找出哪个源文件/源文件中的位置?
\n我是否需要更改某些环境变量的值,例如MallocStackLogging或MallocStackLoggingNoCompact?
我花了一段时间,但一旦我弄清楚了这一切,效果就很好:
valgrind, 直到它无法在我的新 osx 版本上运行。我一直在寻找一种类似方便的命令行方法来跟踪内存泄漏(可以使用 Instruments 代替,但它是重量级的并且是 UI 驱动的,这两者都让我烦恼)leaks -atExit告诉我存在哪些泄漏,但不告诉我泄漏的分配来自哪里leaks -atExit显然是在退出时运行因此,您必须运行 MallocStackLogging,暂停程序,然后运行泄漏:
打开终端并设置 MallocStackLogging:export MallocStackLogging=1
在程序的末尾,在它存在之前,添加一行代码通过从 stdin 读取来暂停它,然后重新编译:fscanf(stdin, "c"); // wait for user to enter input from keyboard
运行你的程序并等待它暂停
在单独的终端中,通过运行输出泄漏分配leaks my_program_name(或者,找到您的 pid:ps aux | grep my_program_name然后运行leaks <pid>)。
干杯