Say I write plain C code using Xcode 5. What facilities does Xcode 5 have to help developer find/detect memory leaks (for C code)? I heard there is some static analysis tool - does it come automatically with Xcode 5? How reliable that is?
Asked
Active
Viewed 2,611 times
2
-
Valgrind is all you need. – Vitaly Isaev Mar 23 '14 at 20:47
-
@VitalyIsaev if you have it running natively on OSX sans-macports i'm impressed. – WhozCraig Mar 23 '14 at 20:47
-
2You might try [Instruments](http://stackoverflow.com/questions/8248345/understanding-instruments-in-xcode-testing-for-leaks), documented on the Apple developer website and multiple questions on this site. And yes, it comes with the Xcode developer tools. – WhozCraig Mar 23 '14 at 20:49
-
1@WhozCraig, http://valgrind.org/info/platforms.html - I'm observing MacOS here. Please correct me if I'm wrong... – Vitaly Isaev Mar 23 '14 at 20:50
-
@VitalyIsaev If its supported now its recent. Regardless, you're absolutely correct that if you can use it by all means do so. It is the absolute shiznit for tracking down leaks and shouldn't-be-doing-thats. – WhozCraig Mar 23 '14 at 20:51
-
@VitalyIsaev: [The front page](http://valgrind.org/) says "Mac OS X 10.7, with limited support for 10.8". OS X 10.9 is totally unsupported, it seems. – Cornstalks Mar 23 '14 at 20:52
-
@WhozCraig: Is Instruments also for C code? – Mar 23 '14 at 20:52
-
@dmcr_code it can do anything, but it isn't going to be as oh my god-thats-amazing as Valgrind. Its more of a polling architecture. I.e. run your program and it leaks a boatload of ram then exits, if Instruments didn't poll-snapshot at the right time it will miss your leak. I have to be honest, for most C code I write it with portability in mind on my Mac, then build it on a Debian box and turn Valgrind loose on it. I find Instruments helpful, particularly for Objective C code, but for most of the stuff I do there is no substitute for the Secretariat of watchdogs. – WhozCraig Mar 23 '14 at 20:58
-
There are some good how-to's around the web for using Instruments with C/C++ code. Do some hunting. One of them I linked, a question on this site, which more than anything else turns you to the WWDC conference slides and demos on effective Instruments usage. its worth a look. – WhozCraig Mar 23 '14 at 21:00
-
@WhozCraig: Ok, I tried Instruments -> Leaks. It indeed reported some 'living objects' which were objects which I didn't free. The static analysis tool on the other hand, missed this leak (I implemented dynamic array and static analysis missed when I deleted the "for" loop inside the "freeArray" method as here for example: http://codereview.stackexchange.com/questions/44649/dynamic-array-of-structs-in-c). Instruments tool like I said, spotted this by reporting 'living objects'. – Mar 23 '14 at 21:02
-
@WhozCraig: Some links how to use Instruments with C/C++ would be nice. – Mar 23 '14 at 21:03
-
The best-maintained and up-to-date ones will be on the Apple Developer Website, including the WWDC content. It requires developer access, which I assume you have (its like $99/year). Its where I go whenever i need to remind myself how to do something with both Instruments and Xcode. – WhozCraig Mar 23 '14 at 21:21
-
@WhozCraig: I had one, not sure on its capabilities though(I mean of account). Otherwise I will (hopefully) find some info on internet... Or use valgrind at some point maybe with Linux. Btw. I got 'impressed' by the check I did now (which I reported in previous comment), and how Instruments->Leaks responded – Mar 23 '14 at 21:24
-
Best bet for Valgrind on OS X 10.8 and above would be to run your code from a Linux VM... – JAL Mar 23 '14 at 22:33
-
There is an excellent static analysis tool ("Analyzer"), but it's only going to work for Objective-C objects, not C mallocs. – Hot Licks Mar 23 '14 at 22:37