You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS Mojave下C语言内存泄漏检测:leaks工具使用异常求助

How to Use the leaks Tool Correctly on macOS Mojave

Got it, let's break down why you're hitting that "unable to read input graph" error and fix it. The core issue here is how you're invoking the leaks tool—it doesn't work by just passing the executable path directly, especially for a program that exits instantly like your Hello World.

Why Your Current Command Fails

Your leaks ./a.out command tries to analyze the executable file directly, but leaks needs to interact with a running process to collect memory data. Since your Hello World program runs and exits in milliseconds, leaks can't grab the necessary process information in time, hence the format error.

Correct Usage Methods

1. Let leaks Launch and Monitor Your Program

Use this command to have leaks start your program and track its memory usage from launch to exit:

leaks -- ./a.out

Add the -quiet flag if you want to cut down on verbose output and only see leak-related info:

leaks -quiet -- ./a.out

The -- is important here—it separates leaks' own arguments from your program's arguments (if you had any), preventing misinterpretation.

For your simple Hello World program, you'll likely see output saying "No leaks detected"—that's normal, since the program doesn't allocate any heap memory (via malloc, calloc, etc.).

2. Attach leaks to a Running Process

If you have a long-running program (e.g., one that waits for user input), you can attach leaks to it after it's started:

  1. First, modify your program to keep running (for testing):
    #include <stdio.h>
    #include <unistd.h>
    
    int main(int argc, char const *argv[]) {
        printf("Hello World\n");
        sleep(30); // Keeps the program alive for 30 seconds
        return 0;
    }
    
  2. Compile and run it in one terminal window.
  3. In another terminal, find the program's Process ID (PID) using:
    ps aux | grep a.out
    
  4. Attach leaks to the PID:
    leaks <YOUR_PROGRAM_PID>
    

Additional Tips

  • Verify Command Line Tools: Double-check you have Xcode Command Line Tools installed with xcode-select --install (you mentioned you do, but it's good to confirm).
  • Grant Debug Permissions: The first time you run leaks, macOS may prompt you to grant Terminal debugging access. If you miss the prompt, go to System Preferences > Security & Privacy > Privacy > Developer Tools and add Terminal to the list of allowed apps.

内容的提问来源于stack exchange,提问作者andrefcptcc34

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:49:57