【问题标题】:Xcode 7.2 & Instruments memory leak not working for c++ codeXcode 7.2 & Instruments 内存泄漏不适用于 c++ 代码
【发布时间】:2016-03-30 05:17:08
【问题描述】:

我使用以下代码:

int main()
{
    int* foo = new int[10];
    foo = nullptr;
    sleep(60);
}

我在 XCode 7.2 中构建它并运行工具来查看内存泄漏。 我以不同的方式更改了时间延迟并更改了仪器检查延迟(从 10 秒到 2 秒)。我尝试以不同的方式构建项目(发布、调试),并且还尝试避免编译器优化:

int* foo = new int[10];
for (int i = 0; i < 10; ++i)
{
    foo[i] = i*3 + i^2;
}

for (int i = 0; i < 10; ++i)
{
    cout << foo[i] << endl;;
}
foo = nullptr;
sleep(60);

但我仍然看不到仪器/泄漏条内的任何泄漏。 我做错了什么?

更新:我找到了一种解决方法,如果我在断点处停止控制台应用程序,然后从控制台运行

MacBook-Pro-andrey-2:~ owl$ leaks Ctrain
Process:         Ctrain [30305]
Path:            /Users/owl/Library/Developer/Xcode/DerivedData/Ctrain-cuhszmbcsswlznetmyijwykgudlz/Build/Products/Debug/Ctrain
Load Address:    0x100000000
Identifier:      Ctrain
Version:         ???
Code Type:       X86-64
Parent Process:  debugserver [30306]

Date/Time:       2015-12-23 21:30:28.768 +0300
Launch Time:     2015-12-23 21:30:25.837 +0300
OS Version:      Mac OS X 10.10.5 (14F27)
Report Version:  7
Analysis Tool:   /Applications/Xcode.app/Contents/Developer/usr/bin/leaks
Analysis Tool Version:  Xcode 7.2 (7C68)
----

leaks Report Version:  2.0
Process 30305: 390 nodes malloced for 34 KB
Process 30305: 1 leak for 48 total leaked bytes.
Leak: 0x1001054f0  size=48  zone: DefaultMallocZone_0x10006e000
    0x00000002 0x00000006 0x0000000a 0x0000000e     ................
    0x00000012 0x00000016 0x0000001a 0x0000001e     ................
    0x00000022 0x00000026 0x93554c2c 0x00007fff     "...&...,LU.....

但这只是解决方法,而不是为什么 Instruments 无法捕获此泄漏(或在我的情况下是任何泄漏)的答案。

【问题讨论】:

  • 你见过this吗?
  • 感谢您的评论,但我也已经尝试过了=( 使用函数调用和 { } 块
  • 是的,我看到了那个帖子。我的时间延迟间隔大于仪器延迟

标签: c++ xcode memory-leaks


【解决方案1】:

是的,这是内存泄漏,因为您将永远无法删除内存,但除非您重做操作,否则您将无法观察到它。如果你有

int main()
{
    int* foo;
    for (i = 0; i < 10; i++)
    {
        foo = new int[10];
        sleep(60);
    }
}

您将能够观察到进程消耗的内存在增加,因为我们在获取更多内存之前从不释放我们请求的内存。

【讨论】:

  • 听起来很奇怪,因为我需要检测一个内存泄漏,但你的示例也没有内存泄漏。也许我需要在 Instruments 或其他东西中为 c++ 设置一些设置?
  • 为什么我永远无法观察到这个泄漏?我可以用 crt marcos for MSVC 来做到这一点..
  • 因为编译器很聪明并且删除了伪代码:foo = new int[10]; :)。你必须使用这块内存。见stackoverflow.com/a/14811858/1387438
猜你喜欢
  • 2011-06-07
  • 2011-10-10
  • 2012-08-22
  • 2013-01-26
  • 1970-01-01
  • 2019-06-20
  • 2023-04-10
  • 2017-05-26
  • 2016-01-10
相关资源
最近更新 更多