无法在xcode 9仪器中检测到C泄漏 [英] Can't detect C leaks in xcode 9 instruments
问题描述
我正在尝试第一次使用乐器.因此,我编写了一个小型C程序来检测仪器中的内存泄漏.
I'm trying to use instruments for the first time. So, I wrote a small C program to detect memory leaks in instruments.
代码:
#include <stdio.h>
#include<stdlib.h>
#include <unistd.h>
int main()
{
int *temp = NULL;
temp = (int*)malloc(100*sizeof(int));
for (int i = 0; i<100; ++i) {
temp[i] = i;
}
printf("%d", *(temp+1));
printf("Hello ");
temp = NULL;
usleep(10000000);
printf("%d", *(temp+1));
}
当我使用 free(temp)
时
在第一个图片中,没有泄漏,但是在下面的面板中,我们可以看到分配的详细信息.
In the 1st pic, there are no leaks but in the below panel we can see the allocated details.
在第二张图片中,没有泄漏,但是在下面的面板中,我们看不到任何细节.
In the 2nd pic, there are no leaks but in the below panel we can see there are no details.
那是为什么?有人可以解释输出内容(顶部和底部面板)吗?
Why is that? Can anybody explain the output(top and below panels)?
谢谢!
更新:
您的意思是这样吗?
int main()
{
char **temp = NULL;
temp = (char**)malloc(100*sizeof(char*));
for (int i = 0; i<100; ++i) {
temp[i] = (char *)malloc(100*sizeof(char));
temp[i]=NULL;
usleep(2000000);
}
}
P.S我标记了C ++,因为我认为上面的代码也可以用C ++编写.如果我输入错了,请删除该标签.
P.S I tagged C++ because I think the above code can be written in C++ also. Please remove the tag if I'm wrong.
推荐答案
您的代码没有问题.如预期的那样,它会导致内存泄漏.问题(实际上是好的)是Xcode.
There is no problem with your code. It creates a memory leak as you expected. The problem(actually its good) is with the Xcode.
Xcode优化您的代码以消除所有内存泄漏.这就是为什么仪器不显示任何内存泄漏的原因.
Xcode optimises your code to remove all the memory leaks. Thats why instruments not showing any memory leaks.
要查看内存泄漏,请禁用Xcode中的优化.
To see your memory leaks, disable the optimisations in the Xcode.
选择无[-O0]
以禁用所有优化.
您可以使用仪器来配置最终的生产代码.因此,请勿更改 Release
设置.您可能会忘记将其更改回原来的位置,并且不会优化您的程序.
You use intstruments to profile the final production code. So, don't change the Release
settings. You may forget to change it back and your program will not be optimised.
相反,将 Profile
的方案从 Release
修改为 Debug
.这样一来,您始终可以获得针对 Release
的优化代码.
Instead edit the scheme of Profile
from Release
to Debug
. Doing this you can always get optimised code for the Release
.
1).单击可执行图标
2).单击 Edit Scheme
.
3).将 Build Configuration
更改为 Debug
.
现在,每当配置文件代码时,由于未对代码进行优化,都会遇到所有错误.
Now, whenever you profile your code, you will get all the errors as your code is not optimised.
要配置您的发布代码,请在 Build Configuration
中将其更改回 Release
.
To profile your release code, change it back to Release
in the Build Configuration
.
这篇关于无法在xcode 9仪器中检测到C泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!