Memory leak detection on Mac OS - c

I’m trying to debug a C code and unfortunately I can’t use valgrind since I have Mavericks installed.
I’m using lldb but am not sure what command to run to check for memory leaks.

If nothing else works, you might try using dmalloc library, which has been ported to OS X and is pretty good at finding reasonably complex memory allocation problems.

Related

Xcode, how many of memory allocations were freed?(Alternative of Valgrind)

I am using Xcode for debugging of my C program, and I want to make sure that all memory allocations has been freed when the program is finished. However, I am not sure how can I see if there is any alloc that is left not freed.
I know that it is possible to do using Valgrind on machines running on Linux. Would appreciate if someone suggests alternative for Mac OS X Yosemite 10.10.1 and it would be even better if there is a way of doing it on Xcode.
When I am looking at memory accumulation (not being released) I use the Xcode profiler and Heap Shot, see For HowTo use Heap Shot (now named Mark Generation) to find memory creap: bbum blog.
Also see the memory diagnostics under "Edit Scheme""Diagnostics": Enable Scribble, Enable Guard Edges, Enable Guard Malloc and Malloc Stack.
valgrind is available on MacOS.
See valgrind web site:
"X86/Darwin and AMD64/Darwin (Mac OS X 10.9, with limited support for 10.8)."
So, that looks a good alternative to valgrind on linux, if you
want to use MacOS.

Can valgrind be used along with Cygwin?

I am building source on Windows using Cygwin, I need to find places with memory leaks and possible memory leaks. I know about Valgrind used for memory leak detection in Linux.
Can Valgrind be used along with Cygwin so that it works in a Windows based environment?
If yes can you please tell what configurations, etc. are needed?
No it's not supported.
Also, an obvious link: http://www.google.com/search?q=cygwin+valgrind

How to detect a memory leak?

I am pretty new to programming and I want to know how to detect a memory leak? If you are going to recommend a utility, please try to find one that works on Mac OS X Lion.
P.S. I tried valgrind, it doesn't work on 10.7/Lion.
Valgrind is an excellent cross platform tool http://valgrind.org/
And best of all its Open Source
I'd recommend Instruments (which comes with Xcode). I admit I've never used it for C, but works well with Objective-C.
valgrind - http://valgrind.org/ is pretty good
or mudflap - http://gcc.gnu.org/wiki/HomePage
but i'm not sure if mudflap works on Mac OS though ,it works on most the linux platform, you could try =)

Best way to find memory leaks in a C program

I am trying to complete a college assignment, and the marking criteria specifies 5% for memory management - specifically for having no memory leaks.
As I understand it, memory leaks in simple C programs, are only caused by pointers which have become abandoned by the program - ie, malloc/calloc/etc calls which are never have a corresponding free.
My question is in 3 parts:
Whats the simplest way on Solaris
and OSX to 'prove' that you haven't
leaked any memory?
Does XCode have
any tools to help determine memory
leaks?
Does the operating system
release all previously allocated
memory within a c program once the
process ends?
Valgrind is your friend.
For every malloc(), you need to ensure that you have exactly one free().
I haven't worked with XCode, but this forum entry may help.
Yes. It's still poor form to let your running program 'leak,' however.
In general, it's a good idea to learn how to avoid leaks without using tools like a memory debugger (early on) -- especially for your simple programs. It's painful, however: when it comes to building anything non-trivial you'll want to start learning how to use the more advanced debugging tools (like Valgrind, as Alex Reynolds suggested in another answer.)
Answer for Mac OS and an example to be avoided (saved you half an hour).
Mac OS doesn't come with Valgrind or dmalloc. Moreover, Valgrind has some compatibility issues when trying to get it installed in Sierra.
There is utility called "leaks", which I get it running by this:
leaks -atExit --/Contents/Developer/usr/lib/libLeaksAtExit.dylib ./a.out
Unfortunately, this doesn't report obvious memory leaks... Maybe I am using it wrong, but I was just searching for an easy way to check that my C program free'd its memory as it should.
If you have time, then maybe read and use Using OSX Leaks for C Programs on the Command Line?
Resources:
Finding Memory Leaks
Using the "leaks" command on a C/C++ executable
PS: Maybe if used with "iprofiler", then it might be useful, but I didn't had it installed.
there's dmalloc too

Can using Valgrind on Linux also help when the app is compiled on Solaris?

I have written a C application that is destined to be on a Solaris SPARC system. However the majority of my development has been on my Mint Linux 64 bit machine.
I have seen I have a small memory leak and have been using Valgrind on Linux to check it out.
My question is: If I run Valgrind on Linux and make all the memory leak stuff 100% (or at least close to that), if I can compile the application on Solaris does it mean that it should also behave the same (meaning I do not have to use Solaris specific memory leak detection tools?)
Reason I ask is I doesn't really have the time to learn all these profiling tools for each platform.....I am hoping that if Valgrind tells me to fix something that the fix will also work on Solaris?
I know the memory management between Linux and Solaris would be quite different, but surely on memory leak detection they would be kind of the same?
Any advice / help would be greatly appreciated ;-)
Lynton
Generally speaking, if the code execution path is the same on both systems and you clean up the memory leaks on one then yes, it will clean them up on the other system too.
It's not quite so straight forward when things differ between the machines though. EG, it tries to do one thing on the linux system and another thing on the solaris system.
Valgrind is a great tool and the best thing to do is try it on linux and see if it cleans up the leaks on solaris too (check with ps or another tool that lists the memory size under solaris and make sure it doesn't increase there).

Resources