Finding where memory was last freed?

前端 未结 3 1914
夕颜
夕颜 2021-01-19 17:26

Very general: Is there an easy way to tell which line of code last freed a block of memory when an access violation occurs?

Less general:

3条回答
  •  心在旅途
    2021-01-19 18:08

    Yes!

    Install the Windows Debugging Tools and use Application Verifier.

    1. File -> Add Application, select your .exe
    2. Under Basics, select Memory and Heaps.
    3. Run the debug build of your program under ntsd (ntsd yourprogram.exe).
    4. Reproduce the bug.

    Now when you make the crash happen, you will get additional information in the debugger from AppVerifier. Use !avrf (may take a long time to run (minutes)) and it will try to give you as much useful information as possible.

    You can all use the dps command on the memory address to get all the stored stack info (allocation, deallocation, etc).

    You can also use the !heap command on the memory address:

    0:004> !heap -p -a 0x0C46CFE0
    

    Which will dump information as well.

    Further Reading:

    • Advanced Windows Debugging, Hewardt and Pravat
    • Debugging with PageHeap

提交回复
热议问题