Valgrind检测内存泄漏而不终止程序

时间:2021-08-18 09:00:10

Is it possible for Valgrind to detect reachable/memory leak without terminating the program?

在不终止程序的情况下,Valgrind是否可能检测到可访问的/内存泄漏?

That is, how to know the result before exiting the program?

也就是说,如何在退出程序之前知道结果?

1 个解决方案

#1


1  

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:

如果您是在GDB下运行程序,那么文档中有一些选项,特别是MemCheck监视器命令一节中提到的其中一个命令是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.
  • 检测[全*|摘要][类型 |可达|可能][heuristics heur1,heur2,…[增加*|修改|any][无限制*|limited ]执行泄漏检查。参数中的*表示默认值。

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.

如果[full*|summary]是summary,则只给出泄漏搜索的摘要;否则将生成完整的泄漏报告。完整的泄漏报告为每次泄漏提供详细信息:分配泄漏块的堆栈跟踪、泄漏块的数量和它们的总大小。在请求完整报告时,接下来的两个参数会进一步指定要报告什么类型的泄漏。如果泄漏的细节与第二个和第三个参数匹配,则显示出来。完整的泄漏报告可能会为许多泄漏输出详细信息。信息输出的泄漏的nr可以使用有限参数控制,然后是泄漏记录输出的最大nr。如果达到这个最大值,泄漏搜索将输出具有最大字节数的记录。

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 value definiteleak is equivalent to kinds definite, the value possibleleak is equivalent to kinds 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).

类型参数控制显示的完全泄漏搜索的块类型。可以使用 指定要显示的泄漏类型集合,类似于命令行选项——显示-泄漏类型。或者,value definiteleak是等价于定义类的,它的值或则是等价于定义类的,可能的:它还会显示可能泄露的块。那些只有一个内部指针的。可达值将显示所有块类别(即等价于所有类型)。

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.

启发式参数控制泄漏搜索中使用的启发式。可以使用 ,类似于命令行选项—leak-checkheuristics来指定使用的启发式。启发式参数的默认值为启发式。

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.

[增加的*|更改了|any]参数控制在完整的泄漏搜索中显示的更改类型。增加的值指定,由于应该显示以前的泄漏检查,所以只有泄漏字节或块数量增加的块分配堆栈才应该显示。值的更改指定了分配堆栈的任何更改,因为应该显示先前的泄漏检查。值any指定应该显示所有泄漏条目,而不考虑任何增加或减少。当指定增加或更改时,泄漏报告条目将显示与先前泄漏报告相对的delta。

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:

如果您想在程序控制下进行,那么下一部分客户端请求将描述一个C程序可以生成的API调用。这些包括:

  • 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.
  • VALGRIND_DO_LEAK_CHECK:现在是否有一个完整的内存泄漏检查(比如,leak-check=full)。这对于增量地检查程序执行中的任意位置之间的泄漏非常有用。它没有返回值。

#1


1  

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:

如果您是在GDB下运行程序,那么文档中有一些选项,特别是MemCheck监视器命令一节中提到的其中一个命令是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.
  • 检测[全*|摘要][类型 |可达|可能][heuristics heur1,heur2,…[增加*|修改|any][无限制*|limited ]执行泄漏检查。参数中的*表示默认值。

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.

如果[full*|summary]是summary,则只给出泄漏搜索的摘要;否则将生成完整的泄漏报告。完整的泄漏报告为每次泄漏提供详细信息:分配泄漏块的堆栈跟踪、泄漏块的数量和它们的总大小。在请求完整报告时,接下来的两个参数会进一步指定要报告什么类型的泄漏。如果泄漏的细节与第二个和第三个参数匹配,则显示出来。完整的泄漏报告可能会为许多泄漏输出详细信息。信息输出的泄漏的nr可以使用有限参数控制,然后是泄漏记录输出的最大nr。如果达到这个最大值,泄漏搜索将输出具有最大字节数的记录。

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 value definiteleak is equivalent to kinds definite, the value possibleleak is equivalent to kinds 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).

类型参数控制显示的完全泄漏搜索的块类型。可以使用 指定要显示的泄漏类型集合,类似于命令行选项——显示-泄漏类型。或者,value definiteleak是等价于定义类的,它的值或则是等价于定义类的,可能的:它还会显示可能泄露的块。那些只有一个内部指针的。可达值将显示所有块类别(即等价于所有类型)。

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.

启发式参数控制泄漏搜索中使用的启发式。可以使用 ,类似于命令行选项—leak-checkheuristics来指定使用的启发式。启发式参数的默认值为启发式。

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.

[增加的*|更改了|any]参数控制在完整的泄漏搜索中显示的更改类型。增加的值指定,由于应该显示以前的泄漏检查,所以只有泄漏字节或块数量增加的块分配堆栈才应该显示。值的更改指定了分配堆栈的任何更改,因为应该显示先前的泄漏检查。值any指定应该显示所有泄漏条目,而不考虑任何增加或减少。当指定增加或更改时,泄漏报告条目将显示与先前泄漏报告相对的delta。

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:

如果您想在程序控制下进行,那么下一部分客户端请求将描述一个C程序可以生成的API调用。这些包括:

  • 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.
  • VALGRIND_DO_LEAK_CHECK:现在是否有一个完整的内存泄漏检查(比如,leak-check=full)。这对于增量地检查程序执行中的任意位置之间的泄漏非常有用。它没有返回值。