0

I'm a C programming student trying to find a way to detect memory leaks on MacOs Mojave.

I know about the existence of Valgrind, but it doesn't support current MacOs releases. After installing Apple Command Line Tools, I tried to run my C program with leaks tool but it doesn´t work... Running this very simple C program:

#include <stdio.h>

int main(int argc, char const *argv[]) {
  printf("Hello World\n");
  return 0;
}

Like this:

leaks ./a.out

This is the output:

leaks[875]: [fatal] unable to read input graph: The data 
 couldn’t be read because it isn’t in the correct format.

I don´t understand why this happens... How can I use the leaks tool?

andrefcptcc34
  • 11
  • 1
  • 1
  • 2

3 Answers3

7

Unlike Valgrind, the leaks command is designed to find memory leaks in code that is already running when the "leaks" command is executed.

In order to get the functionality you are looking for you want the following command:

leaks -atExit -- ./a.out
Arthur Wesley
  • 71
  • 1
  • 4
0

i'm not sure is this helpful, but if the C program's status is in running, "leaks" would be available

#include <stdio.h>

int main(int argc, char const *argv[]) {
  printf("Hello World\n");
  getchar(); // just add to sleep
  return 0;
}

run above, then run belows in another terminal

leaks a.out

you can show related messages

Process:         a.out [8724]
Path:            /Users/USER/Documents/*/a.out
Load Address:    0x1078f2000
Identifier:      a.out
Version:         ???
Code Type:       X86-64
Parent Process:  bash [7876]
...

using [PID] is also available in this example

leaks 8724

here is a sample code that memory leak is detected,

#include <stdio.h>

void test()
{
  char* pTmp = (char*)malloc(sizeof(char)*1);
}

int main(int argc, char const *argv[]) {
  printf("Hello World\n");
  test();
  getchar();
  return 0;
}
boby
  • 137
  • 4
  • I don´t understand how to do that... Could you be more explicit? – andrefcptcc34 Sep 19 '19 at 17:33
  • @andrefcptcc34 open two terminals, first one run ./a.out (including getchar() for running status), then move to another terminal, run leaks a.out – boby Sep 19 '19 at 17:38
  • @andrefcptcc34 simply, just run “top”, and remember one pid in running, then run leaks [PID], like this: leaks 8888 – boby Sep 19 '19 at 17:43
  • Ok, it worked... But why does it happen? Shouldn't it run at first? Using ```leaks ./a.out```? – andrefcptcc34 Sep 19 '19 at 17:44
  • @andrefcptcc34 i tried “leaks ./a.out” + some options, but got a same output message with you, it seems that leaks only working in process level, not a code level – boby Sep 19 '19 at 17:49
  • The leaks tool doesn't show memory leakage when I don't free the malloc... – andrefcptcc34 Sep 19 '19 at 18:59
  • @andrefcptcc34 maybe malloc was in main function which was still on stack, did you try with other test function call including malloc? – boby Sep 19 '19 at 19:07
-2

Try this

#define malloc(X) my_malloc( X, __FILE__, __LINE__, __FUNCTION__)

void* my_malloc(size_t size, const char *file, int line, const char *func)
{

    void *p = malloc(size);
    printf ("Allocated = %s, %i, %s, %p[%li]\n", file, line, func, p, size);

    /*Link List functionality goes in here*/

    return p;
}
champion-runner
  • 1,489
  • 1
  • 13
  • 26