如何使用Instruments(Leaks) - Xcode找到C或C ++代码泄漏? [英] How to find C or C++ code leaks using Instruments (Leaks) - Xcode?

查看:202
本文介绍了如何使用Instruments(Leaks) - Xcode找到C或C ++代码泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int* foo = new int[10];
foo = NULL;
sleep(60);

仪器在上面的代码中没有发现任何泄漏,我如何使用仪器工具查找C或C ++代码泄漏。我有堆栈溢出大多数解释是基于客观C代码...

Instrument is not finding any leak in above code, how do I use Instrument tool to find C or C++ code leaks. I have stack overflowed most of the explanation is based on objective C codes...

推荐答案

问题是编译器会优化出来在以下代码片段中调用 new

The issue is that compiler will optimize out the call to new in the following code fragment:

int* foo = new int[10];
foo = NULL;
sleep(60);

因为它足够聪明,知道它没有被使用。如果您添加代码以使用 foo ,那么编译器将不会执行此操作,您应该会看到预期的泄漏:

as it's smart enough to know that it's not being used. If you add code to use foo then compiler won't do this and you should see the leak you are expecting:

int* foo = new int[10];

foo[3] = 23;
foo[8] = 45;

printf("%d %d\n", foo[3], foo[8]);

foo = NULL;
sleep(60);

这篇关于如何使用Instruments(Leaks) - Xcode找到C或C ++代码泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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