Skip to content

Commit

Permalink
internals/memory-debugging.md: add the how to find a leak chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
bagder committed Feb 17, 2022
1 parent 19c2be7 commit 349b57d
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions internals/memory-debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,56 @@ list 'TrackMemory' feature for curl debug memory tracking capable
builds. These features are independent and can be controlled when running the
configure script. When `--enable-debug` is given both features will be
enabled, unless some restriction prevents memory tracking from being used.

## Track Down Memory Leaks

... using the memory debug system. In general, we suggest using valgrind a the
first choice.

### Single-threaded

Please note that this memory leak system is not adjusted to work in more
than one thread. If you want/need to use it in a multi-threaded app. Please
adjust accordingly.

### Build

Rebuild libcurl with `-DCURLDEBUG` (usually, rerunning configure with
`--enable-debug` fixes this). `make clean` first, then `make` so that all
files are actually rebuilt properly. It will also make sense to build
libcurl with the debug option (usually `-g` to the compiler) so that
debugging it will be easier if you actually do find a leak in the library.

This will create a library that has memory debugging enabled.

### Modify Your Application

Add a line in your application code:

```c
curl_dbg_memdebug("dump");
```
This will make the malloc debug system output a full trace of all resources
using functions to the given file name. Make sure you rebuild your program
and that you link with the same libcurl you built for this purpose as
described above.
### Run Your Application
Run your program as usual. Watch the specified memory trace file grow.
Make your program exit and use the proper libcurl cleanup functions etc. So
that all non-leaks are returned/freed properly.
### Analyze the Flow
Use the `tests/memanalyze.pl` perl script to analyze the dump file:
tests/memanalyze.pl dump
This now outputs a report on what resources that were allocated but never
freed etc. This report is fine for posting to the list.
If this does not produce any output, no leak was detected in libcurl. Then
the leak is mostly likely to be in your code.

0 comments on commit 349b57d

Please sign in to comment.