Check for memory leak using tools on Mac

I just pulled my hair out trying to make Tools cough in my intentionally designed memory leaks. My test case is as follows:

class Leaker { public: char *_array; Leaker() { _array=new char[1000]; } ~Leaker() { } }; void *leaker() { void *p=malloc(1000); int *pa=new int[2000]; { Leaker l; Leaker *pl=new Leaker(); } return p; } int main (int argc, char **argv) { for (int i=0; i<1000; ++i) { leaker(); } sleep(2); // Needed to give Instruments a chance to poll memory return 0; } 

Mostly, the tools never found obvious leaks. I went crazy, but then I found “sec Between Auto Detections” in the “Leakage Configuration” panel of the “Leaks” panel. I scored it as low as it would have been, which was 1 second, and put the dream (2) in my code and voila; leaks found!

As far as I know, a leak is a leak, regardless of whether it occurs 30 minutes in the application or 30 milliseconds. In my case, I canceled the test case to the above code, but my real application is a command line application without an interface or something else, and it runs very quickly; Of course, the default sampling interval is less than 10 seconds.

Okay, so I can live a couple of seconds after exiting my application in toolkit mode, but what I REALLY want is to just have a snapshot memory of the tools on exit, and then do whatever it takes over time time when the application is running.

So ... the question is: is there a way to take tool snapshot memory when the application exits, regardless of the sampling interval?

Greetings

Shane

+4
source share
3 answers

Leaky mode tools can be really powerful for tracking leaks, but I found that it is more biased towards event-based GUI applications than command-line programs (especially those that exit after a short time). There used to be a CHUD API where you could programmatically control toolkit aspects, but the last time I tried, frameworks were no longer provided as part of the SDK. Perhaps some of them are now replaced by Dtrace .

Also, make sure you are up to date with Xcode, as there have been some improvements in this area that could make it easier to do the necessary things. You can also save a short delay before exiting, but make it conditioned by the presence of an environment variable, and then set this environment variable in the tool launch properties for your application, so that working outside the tools does not have a delay.

+3
source

Most unit test codes execute the necessary codes and code outputs. Although this is perfectly normal for unit testing, this poses a problem for the leak tool, which takes time to analyze the process's memory space. To fix this problem, you need to make sure that your device testing code does not exit immediately after completing its tests. You can do this by setting the process indefinitely, instead of the usual exit.

https://developer.apple.com/library/ios/documentation/Performance/Conceptual/ManagingMemory/Articles/FindingLeaks.html

+1
source

I just decided to leave a 2 second delay while building debug + leak.

0
source

Source: https://habr.com/ru/post/1313933/


All Articles