简体   繁体   English

在Xcode 9仪器中无法检测到C泄漏

[英]Can't detect C leaks in xcode 9 instruments

I'm trying to use instruments for the first time. 我正在尝试第一次使用乐器。 So, I wrote a small C program to detect memory leaks in instruments. 因此,我编写了一个小型C程序来检测仪器中的内存泄漏。

Code: 码:

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

在此处输入图片说明


When I use free(temp) 当我使用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)? 谁能解释输出内容(顶部和底部面板)?

Thank you! 谢谢!


Update: 更新:

You mean like this? 你的意思是这样吗?

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

在此处输入图片说明

PS I tagged C++ because I think the above code can be written in C++ also. PS我标记了C ++,因为我认为上面的代码也可以用C ++编写。 Please remove the tag if I'm wrong. 如果我写错了,请删除标签。

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. Xcode优化您的代码以消除所有内存泄漏。 Thats why instruments not showing any memory leaks. 这就是为什么仪器没有显示任何内存泄漏的原因。

To see your memory leaks, disable the optimisations in the Xcode. 若要查看您的内存泄漏,请禁用Xcode中的优化。

在此处输入图片说明

Select None [-O0] to disable all the optimisations. 选择None [-O0]以禁用所有优化。


You use intstruments to profile the final production code. 您可以使用仪器来分析最终的生产代码。 So, don't change the Release settings. 因此,请勿更改Release设置。 You may forget to change it back and your program will not be optimised. 您可能会忘记将其改回,并且不会优化您的程序。

Instead edit the scheme of Profile from Release to Debug . 而是将Profile的方案从ReleaseDebug Doing this you can always get optimised code for the Release . 这样做总是可以得到Release优化代码。

1). 1)。 Click on executable icon 点击可执行图标 在此处输入图片说明

2). 2)。 Click on Edit Scheme . 单击Edit Scheme 在此处输入图片说明

3). 3)。 Change the Build Configuration to Debug . Build Configuration更改为Debug 在此处输入图片说明

Now, whenever you profile your code, you will get all the errors as your code is not optimised. 现在,每当您分析代码时,都将得到所有错误,因为未对代码进行优化。

To profile your release code, change it back to Release in the Build Configuration . 要分析您的发布代码,请在Build Configuration中将其更改回Release

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM