使用Valgrind免费调试后使用

时间:2012-12-02 00:04:04

标签: c++ debugging valgrind

我正在尝试使用Valgrind在我的代码中的免费错误之后调试一个用途。

我的代码在尝试访问以前删除的对象时崩溃。有没有办法看看谁在这种情况下使用Valgrind删除了对象?

我使用以下选项运行Valgrind,但它只捕获崩溃,并显示它发生的位置。我希望得到有关对象被解除分配的详细信息:

  

valgrind --tool = memcheck

2 个答案:

答案 0 :(得分:3)

这是我在这些情况下使用的:

valgrind --track-origins=yes

如果是free-after-free,它将显示释放内存/删除对象的函数的堆栈跟踪。

请阅读Valgrind的手册,了解有关表现的警告。如果您的问题是并发问题,那么较慢的Valgrind可能会更改程序的计时属性,并可能更改(降低或增加)查找错误的可能性。

--track-origins=<yes|no> [default: no]
    Controls whether Memcheck tracks the origin of uninitialised
    values. By default, it does not, which means that although it can
    tell you that an uninitialised value is being used in a dangerous
    way, it cannot tell you where the uninitialised value came from.
    This often makes it difficult to track down the root problem.

    When set to yes, Memcheck keeps track of the origins of all
    uninitialised values. Then, when an uninitialised value error is
    reported, Memcheck will try to show the origin of the value. An
    origin can be one of the following four places: a heap block, a
    stack allocation, a client request, or miscellaneous other sources
    (eg, a call to brk).

    For uninitialised values originating from a heap block, Memcheck
    shows where the block was allocated. For uninitialised values
    originating from a stack allocation, Memcheck can tell you which
    function allocated the value, but no more than that -- typically it
    shows you the source location of the opening brace of the function.
    So you should carefully check that all of the function's local
    variables are initialised properly.

    Performance overhead: origin tracking is expensive. It halves
    Memcheck's speed and increases memory use by a minimum of 100MB,
    and possibly more. Nevertheless it can drastically reduce the
    effort required to identify the root cause of uninitialised value
    errors, and so is often a programmer productivity win, despite
    running more slowly.

    Accuracy: Memcheck tracks origins quite accurately. To avoid very
    large space and time overheads, some approximations are made. It is
    possible, although unlikely, that Memcheck will report an incorrect
    origin, or not be able to identify any origin.

    Note that the combination --track-origins=yes and
    --undef-value-errors=no is nonsensical. Memcheck checks for and
    rejects this combination at startup.

答案 1 :(得分:1)

Valgrind已经向你展示了它的最大值。您需要使用更多调试信息编译代码 - 然后valgrind将能够向您显示更多信息,如文件,函数和行。

使用-g选项编译代码,然后使用valgrind重新运行。在某些编译器上还有-gN,其中N是调试级别。但在大多数情况下,-g就足够了。