查找内存最后被释放的位置?

发布于 2024-07-15 08:54:35 字数 257 浏览 4 评论 0原文

非常笼统: 当发生访问冲突时,是否有一种简单的方法可以判断哪一行代码最后释放了内存块?

不那么笼统: 我对探查器的理解是它们会覆盖分配和释放过程。 如果这是真的,他们是否会碰巧存储最后释放一段内存的代码行,以便当它稍后因访问冲突而崩溃时,您知道最后释放它的是什么?

具体: Windows、ANSI C、使用 Visual Studio

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:
My understanding of profilers is that they override the allocation and deallocation processes. If this is true, might they happen to store the line of code that last freed a section of memory so that when it later crashes because of an access violation, you know what freed it last?

Specifics:
Windows, ANSI C, using Visual Studio

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

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

发布评论

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

评论(3

你不是我要的菜∠ 2024-07-22 08:54:36

不,除非您提供自己的分配器(例如通过重载 new/delete)来存储此信息。

分析器所做的事情很大程度上取决于他们分析的内容。 我不知道有任何分析器可以跟踪您正在寻找的内容。

也许如果您提供有关您的情况的更多详细信息,人们可能会建议一种诊断您遇到的问题的替代方法。

No, not unless you provide your own allocators (e.g. by overloading new/delete) to store this information.

What profilers do is highly dependent on what they're profiling. I'm not aware of any profiler that tracks what you're looking for.

Perhaps if you provided more details on your situation people could suggest an alternative means of diagnosing the problem you're encountering.

吾性傲以野 2024-07-22 08:54:35

是的!

安装Windows 调试工具并使用应用程序验证程序

  1. 文件-> 添加应用程序,选择您的 .exe
  2. 在“基础”下,选择“内存和堆”。
  3. 在 ntsd 下运行程序的调试版本 (ntsd yourprogram.exe)。
  4. 重现该错误。

现在,当您发生崩溃时,您将在调试器中从 AppVerifier 获得更多信息。 使用!avrf(可能需要很长时间才能运行(分钟)),它会尝试为您提供尽可能多的有用信息。

您都可以在内存地址上使用 dps 命令来获取所有存储的堆栈信息(分配、释放等)。

您还可以在内存地址上使用 !heap 命令:

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

这也会转储信息。

进一步阅读:

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:

遗弃M 2024-07-22 08:54:35

简短的回答:不。

您需要的是调试 malloc。 我不再关注 Windows,但有几个关于,包括 这个免费的

更新

看起来 Visual Studio C 有一个内置版本。 请参阅此处

当应用程序链接到
C 运行时的调试版本
库,malloc 解析为
_malloc_dbg。 有关如何管理堆的更多信息
调试过程参见CRT
调试堆。

...并参见此处的 _malloc_dbg

Short answer: no.

What you need is a debug malloc. I don't keep up with Windows any longer but there are several about, including this free one.

Update

Looks like Visual Studio C has a built in version. See here

When the application is linked with a
debug version of the C run-time
libraries, malloc resolves to
_malloc_dbg. For more information about how the heap is managed during
the debugging process, see The CRT
Debug Heap.

... and see here for _malloc_dbg.

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