Is it possible for Valgrind to detect reachable/memory leak without terminating the program?
That is, how to know the result before exiting the program?
If you're running the program under GDB, then there are some options described in the documentation, notably the section on MemCheck Monitor Commands, Specifically, one of the commands mentioned is leak_check
:
leak_check [full*|summary] [kinds <set>|reachable|possibleleak*|definiteleak] [heuristics heur1,heur2,...] [increased*|changed|any] [unlimited*|limited <max_loss_records_output>]
performs a leak check. The * in the arguments indicates the default values.If the
[full*|summary]
argument is summary, only a summary of the leak search is given; otherwise a full leak report is produced. A full leak report gives detailed information for each leak: the stack trace where the leaked blocks were allocated, the number of blocks leaked and their total size. When a full report is requested, the next two arguments further specify what kind of leaks to report. A leak's details are shown if they match both the second and third argument. A full leak report might output detailed information for many leaks. The nr of leaks for which information is output can be controlled using the limited argument followed by the maximum nr of leak records to output. If this maximum is reached, the leak search outputs the records with the biggest number of bytes.The kinds argument controls what kind of blocks are shown for a full leak search. The set of leak kinds to show can be specified using a
<set>
similarly to the command line option--show-leak-kinds
. Alternatively, the valuedefiniteleak
is equivalent tokinds definite
, the valuepossibleleak
is equivalent tokinds definite,possible
: it will also show possibly leaked blocks, .i.e those for which only an interior pointer was found. The value reachable will show all block categories (i.e. is equivalent to kinds all).The heuristics argument controls the heuristics used during the leak search. The set of heuristics to use can be specified using a
<set>
similarly to the command line option--leak-check-heuristics
. The default value for the heuristics argument is heuristics none.The
[increased*|changed|any]
argument controls what kinds of changes are shown for a full leak search. The value increased specifies that only block allocation stacks with an increased number of leaked bytes or blocks since the previous leak check should be shown. The value changed specifies that allocation stacks with any change since the previous leak check should be shown. The value any specifies that all leak entries should be shown, regardless of any increase or decrease. When If increased or changed are specified, the leak report entries will show the delta relative to the previous leak report.
Etc.
If you want to do it under program control, then the next section Client Requests describes the API calls that a C program can make. These include:
- VALGRIND_DO_LEAK_CHECK: does a full memory leak check (like
--leak-check=full
) right now. This is useful for incrementally checking for leaks between arbitrary places in the program's execution. It has no return value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With