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?
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;
}
#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;
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.