3

Valgrindを使用して、コードのフリーエラー後の使用をデバッグしようとしています。

以前に削除されたオブジェクトにアクセスしようとすると、コードがクラッシュします。この場合、Valgrindを使用して誰がオブジェクトを削除したかを確認する方法はありますか?

次のオプションを使用してValgrindを実行しましたが、クラッシュをキャッチするだけで、発生した場所が表示されます。オブジェクトの割り当てが解除された場所の詳細を取得したいと思っています。

valgrind --tool = memcheck

4

2 に答える 2

6

これは私がこれらの場合に使用するものです:

valgrind --track-origins=yes

use-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.
于 2012-12-02T01:06:00.623 に答える
1

Valgrindは、可能な限り最大限の成果を上げています。より多くのデバッグ情報を使用してコードをコンパイルする必要があります。そうすると、valgrindはファイル、関数、行などのより多くの情報を表示できるようになります。

オプションを使用してコードをコンパイル-gし、valgrindを使用して再実行します。N一部のコンパイラには、デバッグのレベルである-gNもあります。しかし、ほとんどの場合、-gそれで十分です。

于 2012-12-02T00:21:56.630 に答える