Xcode 7.2及以上仪器内存泄漏不适用于C ++代码 [英] Xcode 7.2 & Instruments memory leak not working for c++ code

查看:63
本文介绍了Xcode 7.2及以上仪器内存泄漏不适用于C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

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

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

I build it inside XCode 7.2 and run instruments to see memory leak. I changed time delay differently and changed Instruments checking delay (from 10 to 2 sec). I tried to build project differently (Release, Debug) and also I tried to avoid compiler optimization:

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);

但是我仍然看不到仪器/泄漏条内部有任何泄漏. 我做错了什么?

but I still can't see any leak inside Instruments/leak bars. What I do wrong?

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

Upd: I found a workaround, if I stop my console app at breakpoint and then, from the console, run

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无法捕获此泄漏(或我的情况下的任何泄漏)的答案.

but it is just workaround and not the answer why Instruments can't catch this leak (or any leak in my case).

推荐答案

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

Yes this is a memory leak as you will never be able to delete the memory but you will not be able to observe it unless you redo the operation. If you had

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

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

You would be able to observer the memory being consumed by the process increasing as we never release the memory we request before grabbing more memory.

这篇关于Xcode 7.2及以上仪器内存泄漏不适用于C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆