[英]How to use leaks command-line tool to find memory leaks?
leaks
命令行工具将报告
像下面这样:
Process: checker [84357]
Path: /path/to/program
Load Address: 0x104703000
Identifier: checker
Version: ???
Code Type: X86-64
Parent Process: zsh [64610]
Date/Time: 2019-11-30 18:43:06.864 -0800
Launch Time: 2019-11-30 18:42:58.593 -0800
OS Version: Mac OS X 10.13.4 (17E199)
Report Version: 7
Analysis Tool: /usr/bin/leaks
Physical footprint: 300K
Physical footprint (peak): 300K
----
leaks Report Version: 3.0
Process 84357: 161 nodes malloced for 17 KB
Process 84357: 3 leaks for 64 total leaked bytes.
Leak: 0x7fdf5b400350 size=16 zone: DefaultMallocZone_0x10470e000
Leak: 0x7fdf5b4027c0 size=16 zone: DefaultMallocZone_0x10470e000
Leak: 0x7fdf5b402810 size=32 zone: DefaultMallocZone_0x10470e000
我的问题是,我如何使用这些信息来实际追踪并找到我的源代码中哪些 malloc 调用没有相应的free()
调用?
如何找出源文件中的哪个源文件/位置?
我是否需要更改某些环境变量的值,例如MallocStackLogging
或MallocStackLoggingNoCompact
?
将环境变量MallocStackLogging
设置为 true,运行程序,然后运行leaks
。
这将打印泄漏内存分配位置的堆栈跟踪。
我这样做的方式是:
export MallocStackLogging=1
main
函数中,在返回之前添加以下代码。system("leaks executablename");
.我花了一段时间,但是一旦我想通了这一切,它就很好用:
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
fscanf(stdin, "c"); // wait for user to enter input from keyboard
运行你的程序并等待它暂停
在单独的终端中,找到您的 pid: ps aux | grep my_program_name
ps aux | grep my_program_name
运行泄漏,现在它将输出泄漏分配: leak <pid_from_above_step>
干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.