How to find C or C++ code leaks using Instruments

2019-04-13 00:52发布

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

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...

1条回答
来,给爷笑一个
2楼-- · 2019-04-13 01:36

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

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);
查看更多
登录 后发表回答