如何使用 MacOS Monterey 12.0.1 泄漏查找 Clion c++ 中内存泄漏的确切代码行?

问题描述 投票:0回答:1

我正在使用 Clion,我想找到一种方法来查找 Mac M1 上的内存泄漏。尚不支持 Valgrind。

让我们看一下这个存在内存泄漏的简单代码:

// Program with memory leak

#include <cstdlib>

using namespace std;

// function with memory leak
void func_to_show_mem_leak()
{
    int* ptr = new int(5);

    // body

    // return without deallocating ptr
    return;
}

// driver code
int main()
{

    // Call the function
    // to get the memory leak
    func_to_show_mem_leak();

    system("leaks SimpleMemoryLeak");
    return 0;
}

我尝试过使用leaks

leaks *NameOfProccess*

我得到这个输出:

Process 70526 is not debuggable. Due to security restrictions, leaks can only show or save contents of readonly memory of restricted processes.

Process:         SimpleMemoryLeak [70526]
Path:            /Users/USER/Documents/*/SimpleMemoryLeak
Load Address:    0x100ec8000
Identifier:      SimpleMemoryLeak
Version:         ???
Code Type:       ARM64
Parent Process:  clion [39550]

Date/Time:       2021-12-05 23:32:52.942 +0100
Launch Time:     2021-12-05 23:32:52.307 +0100
OS Version:      macOS 12.0.1 (21A559)
Report Version:  7
Analysis Tool:   /usr/bin/leaks

Physical footprint:         945K
Physical footprint (peak):  945K
----

leaks Report Version: 4.0
Process 70526: 208 nodes malloced for 12 KB
Process 70526: 1 leak for 16 total leaked bytes.

    1 (16 bytes) ROOT LEAK: 0x6000003ac030 [16]

有什么方法可以获取malloc所在的确切代码行吗?谢谢!

c++ clion apple-m1 memory-leak-detector
1个回答
0
投票

导出 MallocStackLogging=1

这将向您显示报告

© www.soinside.com 2019 - 2024. All rights reserved.