Here, two different methods are presented to analyse memory leaks in C code.
The first method is cross-platform. It uses a small Docker image to both run Valgrind on the code and execute the code with the AddressSanitizer.
The second method is exclusive to macOS. It runs leaks
on the code and is a bit simpler than the first method in that it does not use Docker. leaks
should be as performant as Valgrind (although Valgrind has more features and could potentially catch memory leaks that leaks
cannot catch).
- Append the content of the
canalysis.sh
file to your~/.zshrc
,~/.bashrc
, or equivalent file for your shell- For zsh, this can be done with
cat canalysis.sh >> ~/.zshrc
- For zsh, this can be done with
- Install Docker
- Place the
Dockerfile
in the directory of your choice - While in the directory containing the
Dockerfile
, execute:docker build -t "canalysis" .
- The Docker image will initialize and download the dependencies
The syntax for the new canalysis
command is as follows:
canalysis <file1.c> <file2.c> <file3.c> <...>
All of the files passed as arguments will be directly given to clang to be compiled. Note that the generated executable will have the name of
<file1>
.
canalysis
can be used in your terminal from any directory to automatically and properly compile the .c
files passed as arguments, run Valgrind on the generated executable and execute the code with the AddressSanitizer.
The output of the analyses will be found in:
./analysis/sanitizer_output.txt
./analysis/valgrind_output.txt
.
- Remove the appended function from your
~/.zshrc
,~/.bashrc
, or equivalent file for your shell - Remove the file
Dockerfile
- Remove the Docker image
- The following command should work:
docker rmi canalysis
, but if it doesnt' force it withdocker rmi -f canalysis
- The following command should work:
Append the content of the cleaks.sh
file to your ~/.zshrc
, ~/.bashrc
, or equivalent file for your shell
- For zsh, this can be done with
cat cleaks.sh >> ~/.zshrc
The syntax for the new cleaks
command is as follows:
cleaks <file1.c> <file2.c> <file3.c> <...>
All of the files passed as arguments will be directly given to clang to be compiled. Note that the generated executable will have the name of
<file1>
.
cleaks
can be used in your terminal from any directory to automatically and properly compile the .c
files passed as arguments and run leaks
on the generated executable.
The output of the analysis will be found in:
./analysis/leaks_output.txt
Remove the appended function from your ~/.zshrc
, ~/.bashrc
, or equivalent file for your shell