将 --memcheck 限制为您自己的代码

发布于 2024-11-29 11:21:00 字数 4156 浏览 2 评论 0原文

假设我正在使用一个使用 glibc 的库。当我在通过 Valgrind 运行程序时退出程序时,Valgrind 会检测到各种内存泄漏。我 100% 确定没有任何泄漏与我刚刚编写的几行代码明确相关。有没有办法抑制其他库的泄漏,并将泄漏检测限制在您的直接代码中?

例如:

valgrind --tool=memcheck --leak-check=full --leak-resolution=high \
    --log-file=vgdump ./Main

可执行文件是从以下源构建的:

// Include header files for application components.
#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget window;
    window.resize( 320,240 );
    window.setWindowTitle(
        QApplication::translate( "toplevel", "Top-level Widget" ) );
    window.show( );

    QPushButton button(                   
        QApplication::translate( "childwidget", "Press me"), &window );
    button.move( 100, 100 );
    button.show( );
    int status = app.exec();
    return status;
}

具有报告以下内容的日志文件(删除了大部分):

   ==12803== Memcheck, a memory error detector
   ==12803== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
   ==12803== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
   ==12803== Command: ./Main
   ==12803== Parent PID: 12700
   ==12803==
   ==12803==
   ==12803== HEAP SUMMARY:
   ==12803==     in use at exit: 937,411 bytes in 8,741 blocks
   ==12803==   total heap usage: 38,227 allocs, 29,486 frees, 5,237,254 bytes allocated
   ==12803==
   ==12803== 1 bytes in 1 blocks are possibly lost in loss record 1 of 4,557
   ==12803==    at 0x402577E: malloc (vg_replace_malloc.c:195)
   ==12803==    by 0xA1DFA4: g_malloc (in /lib/libglib-2.0.so.0.2600.0)
   ==12803==    by 0xA37F29: g_strdup (in /lib/libglib-2.0.so.0.2600.0)
   ==12803==    by 0xB2A6FA: g_param_spec_string (in /lib/libgobject-2.0.so.0.2600.0)
   ==12803==    by 0x41F36473: ??? (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
   ==12803==    by 0xB3D237: g_type_class_ref (in /lib/libgobject-2.0.so.0.2600.0)
   ==12803==    by 0xB20B38: g_object_newv (in /lib/libgobject-2.0.so.0.2600.0)
   ==12803==    by 0xB212EF: g_object_new (in /lib/libgobject-2.0.so.0.2600.0)
   ==12803==    by 0x41F34857: gtk_settings_get_for_screen (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
   ==12803==    by 0x41ED0CB6: ??? (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
   ==12803==    by 0xB377C7: g_cclosure_marshal_VOID__OBJECT (in /lib/libgobject-2.0.so.0.2600.0)
   ==12803==    by 0xB1ABE2: g_closure_invoke (in /lib/libgobject-2.0.so.0.2600.0)
    ...

    ==12803== 53,244 bytes in 29 blocks are possibly lost in loss record 4,557 of 4,557
    ==12803==    at 0x402577E: malloc (vg_replace_malloc.c:195)
    ==12803==    by 0xA1DFA4: g_malloc (in /lib/libglib-2.0.so.0.2600.0)
    ==12803==    by 0xA36050: g_slice_alloc (in /lib/libglib-2.0.so.0.2600.0)
    ==12803==    by 0xA36315: g_slice_alloc0 (in /lib/libglib-2.0.so.0.2600.0)
    ==12803==    by 0xB40077: g_type_create_instance (in /lib/libgobject-2.0.so.0.2600.0)
    ==12803==    by 0xB1CE35: ??? (in /lib/libgobject-2.0.so.0.2600.0)
    ==12803==    by 0xB205C6: g_object_newv (in /lib/libgobject-2.0.so.0.2600.0)
    ==12803==    by 0xB212EF: g_object_new (in /lib/libgobject-2.0.so.0.2600.0)
    ==12803==    by 0x6180FA3: ??? (in /usr/lib/gtk-2.0/2.10.0/engines/libclearlooks.so)
    ==12803==    by 0x41F0CDDD: ??? (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
    ==12803==    by 0x41F11C24: gtk_rc_get_style (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
    ==12803==    by 0x4200A81F: ??? (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
    ==12803==
    ==12803== LEAK SUMMARY:
    ==12803==    definitely lost: 2,296 bytes in 8 blocks
    ==12803==    indirectly lost: 7,720 bytes in 382 blocks
    ==12803==      possibly lost: 509,894 bytes in 2,908 blocks
    ==12803==    still reachable: 417,501 bytes in 5,443 blocks
    ==12803==         suppressed: 0 bytes in 0 blocks
    ==12803== Reachable blocks (those to which a pointer was found) are not shown.
    ==12803== To see them, rerun with: --leak-check=full --show-reachable=yes
    ==12803==
    ==12803== For counts of detected and suppressed errors, rerun with: -v
    ==12803== ERROR SUMMARY: 1364 errors from 1364 contexts (suppressed: 122 from 11)

Lets say I am using a library that uses glibc. When I exit the program while running it through Valgrind all sorts of memory leaks are detected by Valgrind. I am 100% sure that none of the leaks are explicitly related to my few lines of code I just wrote. Is there a way to suppress leaks from other libraries, and limit the leak detection to your immediate code?

For example:

valgrind --tool=memcheck --leak-check=full --leak-resolution=high \
    --log-file=vgdump ./Main

Where the executable was built from the following source:

// Include header files for application components.
#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget window;
    window.resize( 320,240 );
    window.setWindowTitle(
        QApplication::translate( "toplevel", "Top-level Widget" ) );
    window.show( );

    QPushButton button(                   
        QApplication::translate( "childwidget", "Press me"), &window );
    button.move( 100, 100 );
    button.show( );
    int status = app.exec();
    return status;
}

Has a log-file that reports the following (large portions removed):

   ==12803== Memcheck, a memory error detector
   ==12803== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
   ==12803== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
   ==12803== Command: ./Main
   ==12803== Parent PID: 12700
   ==12803==
   ==12803==
   ==12803== HEAP SUMMARY:
   ==12803==     in use at exit: 937,411 bytes in 8,741 blocks
   ==12803==   total heap usage: 38,227 allocs, 29,486 frees, 5,237,254 bytes allocated
   ==12803==
   ==12803== 1 bytes in 1 blocks are possibly lost in loss record 1 of 4,557
   ==12803==    at 0x402577E: malloc (vg_replace_malloc.c:195)
   ==12803==    by 0xA1DFA4: g_malloc (in /lib/libglib-2.0.so.0.2600.0)
   ==12803==    by 0xA37F29: g_strdup (in /lib/libglib-2.0.so.0.2600.0)
   ==12803==    by 0xB2A6FA: g_param_spec_string (in /lib/libgobject-2.0.so.0.2600.0)
   ==12803==    by 0x41F36473: ??? (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
   ==12803==    by 0xB3D237: g_type_class_ref (in /lib/libgobject-2.0.so.0.2600.0)
   ==12803==    by 0xB20B38: g_object_newv (in /lib/libgobject-2.0.so.0.2600.0)
   ==12803==    by 0xB212EF: g_object_new (in /lib/libgobject-2.0.so.0.2600.0)
   ==12803==    by 0x41F34857: gtk_settings_get_for_screen (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
   ==12803==    by 0x41ED0CB6: ??? (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
   ==12803==    by 0xB377C7: g_cclosure_marshal_VOID__OBJECT (in /lib/libgobject-2.0.so.0.2600.0)
   ==12803==    by 0xB1ABE2: g_closure_invoke (in /lib/libgobject-2.0.so.0.2600.0)
    ...

    ==12803== 53,244 bytes in 29 blocks are possibly lost in loss record 4,557 of 4,557
    ==12803==    at 0x402577E: malloc (vg_replace_malloc.c:195)
    ==12803==    by 0xA1DFA4: g_malloc (in /lib/libglib-2.0.so.0.2600.0)
    ==12803==    by 0xA36050: g_slice_alloc (in /lib/libglib-2.0.so.0.2600.0)
    ==12803==    by 0xA36315: g_slice_alloc0 (in /lib/libglib-2.0.so.0.2600.0)
    ==12803==    by 0xB40077: g_type_create_instance (in /lib/libgobject-2.0.so.0.2600.0)
    ==12803==    by 0xB1CE35: ??? (in /lib/libgobject-2.0.so.0.2600.0)
    ==12803==    by 0xB205C6: g_object_newv (in /lib/libgobject-2.0.so.0.2600.0)
    ==12803==    by 0xB212EF: g_object_new (in /lib/libgobject-2.0.so.0.2600.0)
    ==12803==    by 0x6180FA3: ??? (in /usr/lib/gtk-2.0/2.10.0/engines/libclearlooks.so)
    ==12803==    by 0x41F0CDDD: ??? (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
    ==12803==    by 0x41F11C24: gtk_rc_get_style (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
    ==12803==    by 0x4200A81F: ??? (in /usr/lib/libgtk-x11-2.0.so.0.2200.0)
    ==12803==
    ==12803== LEAK SUMMARY:
    ==12803==    definitely lost: 2,296 bytes in 8 blocks
    ==12803==    indirectly lost: 7,720 bytes in 382 blocks
    ==12803==      possibly lost: 509,894 bytes in 2,908 blocks
    ==12803==    still reachable: 417,501 bytes in 5,443 blocks
    ==12803==         suppressed: 0 bytes in 0 blocks
    ==12803== Reachable blocks (those to which a pointer was found) are not shown.
    ==12803== To see them, rerun with: --leak-check=full --show-reachable=yes
    ==12803==
    ==12803== For counts of detected and suppressed errors, rerun with: -v
    ==12803== ERROR SUMMARY: 1364 errors from 1364 contexts (suppressed: 122 from 11)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-12-06 11:21:00

忽略任何 lib 目录(/lib/lib64<下的所有共享库中的泄漏错误/em>, /usr/lib, /usr/lib64, ...),将其放入文件中并使用 --suppressions 将其传递给 valgrind =*FILENAME*

{
   ignore_unversioned_libs
   Memcheck:Leak
   ...
   obj:*/lib*/lib*.so
}
{
   ignore_versioned_libs
   Memcheck:Leak
   ...
   obj:*/lib*/lib*.so.*
}

这可能会只需将 memcheck 报告限制为您自己的代码即可。但是,请注意,这将忽略由您编写的库调用的任何回调引起的错误。捕获这些回调中的错误几乎可以通过以下方式完成:

{
   ignore_unversioned_libs
   Memcheck:Leak
   obj:*/lib*/lib*.so
   ...
   obj:*/lib*/lib*.so
}
{
   ignore_versioned_libs
   Memcheck:Leak
   obj:*/lib*/lib*.so.*
   ...
   obj:*/lib*/lib*.so.*
}

...但这揭示了使用 Valgrind malloc 的库调用中的错误。由于 valgrind malloc 直接注入到程序文本中(而不是作为动态库加载),因此它在堆栈中的显示方式与您自己的代码相同。这使得 Valgrind 能够跟踪分配情况,但也使得准确执行您所要求的操作变得更加困难。

仅供参考:我正在使用 valgrind 3.5。

以上是对这个问题的正文中提出的一个较旧的、略有不同的问题的回答的摘录(因此标题有点不够):

To ignore Leak errors in all shared libraries under any lib directory (/lib, /lib64, /usr/lib, /usr/lib64, ...), put this in a file and pass it to valgrind with --suppressions=*FILENAME*:

{
   ignore_unversioned_libs
   Memcheck:Leak
   ...
   obj:*/lib*/lib*.so
}
{
   ignore_versioned_libs
   Memcheck:Leak
   ...
   obj:*/lib*/lib*.so.*
}

This will probably suffice to limit memcheck reporting to your own code only. However, beware that this will ignore errors caused by any callbacks you wrote that were invoked by the libraries. Catching errors in those callbacks could almost be done with:

{
   ignore_unversioned_libs
   Memcheck:Leak
   obj:*/lib*/lib*.so
   ...
   obj:*/lib*/lib*.so
}
{
   ignore_versioned_libs
   Memcheck:Leak
   obj:*/lib*/lib*.so.*
   ...
   obj:*/lib*/lib*.so.*
}

... but this reveals errors in calls by a library that use the Valgrind malloc. Since valgrind malloc is injected directly into the program text -- not loaded as a dynamic library -- it appears in the stack the same way as your own code does. This allows Valgrind to track the allocations, but also makes it harder to do exactly what you have asked.

FYI: I am using valgrind 3.5.

The above is an excerpt of an answer to an older, slightly different question that is asked in the body text of this question (so title is a little insufficient):

暖心男生 2024-12-06 11:21:00

在以下位置查找抑制主题: Valgrind 网站;您想抑制来自第三方库的错误。

Look up the topic of suppressions at the Valgrind web site; you want to suppress errors from the third party library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文