软件可以跟踪旧项目中的几个内存错误?

发布于 2024-08-20 16:36:16 字数 212 浏览 11 评论 0原文

我从两年前开始编写游戏程序。 有时一些内存错误(即:函数返回垃圾而不是它应该返回的内容,或者只发生在 Linux 上,而不会发生在 GDB 或 Windows 上的崩溃)似乎是随机发生的。也就是说,我尝试修复它,但几个月后,同样的错误又再次困扰着我。

有一个软件(不是 Valgrind,我已经尝试过......它没有发现错误)可以帮助我解决这个问题?或者解决这些错误的方法?我想永久修复它们。

I am programming a game since 2 years ago.
sometimes some memory errors (ie: a function returning junk instead of what it was supposed to return, or a crash that only happen on Linux, and never happen with GDB or Windows) happen seemly at random. That is, I try to fix it, and some months later the same errors return to haunt me.

There are a software (not Valgrind, I already tried it... it does not find the errors) that can help me with that problem? Or a method of solving these errors? I want to fix them permanently.

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

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

发布评论

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

评论(5

栖迟 2024-08-27 16:36:16

在 Windows 上,您可以自动捕获生产环境中的崩溃异常并对其进行分析,就像错误发生在调试器下的开发人员 PC 上一样。这是使用“小型转储”文件完成的。您基本上使用 Windows“dbghelp.dll”DLL 来生成线程堆栈、部分或全部堆、寄存器值、加载的模块以及导致崩溃的未处理异常的副本。您可以在 MS Visual Studio 调试器中启动此“.dmp”文件,就好像它是可执行文件一样,它将准确显示崩溃发生的位置。

您可以设置针对未处理的异常的陷阱 并将小型转储文件的创建委托给该陷阱中的 dbghelp.dll。您需要保留使用已部署的二进制文件生成的“.pdb”文件,以将内存地址与源代码位置相匹配,以获得更好的调试体验。该主题太深,无法完全涵盖,请参阅 Microsoft 文档< /a> 在此 DLL 上。

您确实需要能够将 .dmp 文件从崩溃的 PC 复制到您的开发环境中以对其进行完全调试。如果您与用户之间存在不干涉关系,则需要选择通过互联网使用单独的实用程序应用程序“phone home”,以将 .dmp 文件传输到您可以访问的位置。生成 .dmp 文件后,您可以从未处理的异常陷阱启动应用程序。为了用户隐私,您应该让用户选择是否这样做。

On Windows, you can automatically capture a crashing exception in a production environment and analyze it as if the error occurred on your developer PC under the debugger. This is done using a "mini-dump" file. You basically use the Windows "dbghelp.dll" DLL to generate a copy of the thread stacks, parts or all of the heap, the register values, the loaded modules, and the unhandled exception that resulted in the crash. You can launch this ".dmp" file in the MS Visual Studio debugger as if it were an executable and it will show you exactly where the crash occurred.

You can set up a trap for unhandled exceptions and delegate the creation of the mini-dump file to dbghelp.dll in that trap. You need to keep the ".pdb" files that were generated with the deployed binaries to match up memory addresses with source code locations for a better debugging experience. This topic is too deep to fully cover See Microsoft's documentation on this DLL.

You do need to be able to copy the .dmp file from the PC where it crashed to your development environment to fully debug it. If you have a hands-off relationship with your users you'll need to have the option of having a separate utility app "phone home" over the internet to tranfer the .dmp file to a location where you can access it. You can launch the app from the unhandled exception trap after the .dmp file has been generated. For user privacy, you should give the user the option of whether or not to do this.

昨迟人 2024-08-27 16:36:16

Totalview 调试器(商业软件)可能会捕获崩溃。

Purify(商业软件)可以帮助您发现内存泄漏。

您的代码编译时是否没有编译器警告?你跑过 lint 吗?

The Totalview debugger (commercial software) may catch the crash.

Purify (commercial software) can help you find memory leaks.

Does your code compile free of compiler warnings? Did you run lint?

诺曦 2024-08-27 16:36:16

您可以尝试的一件事是在您的项目中使用 Hans Boehm GC。它可以用作泄漏检测器,允许您删除可疑的 free()delete 语句,并轻松查看它们是否导致内存泄漏。

One thing you could try is using the Hans Boehm GC with your project. It can be used as a leak detector, allowing you to remove suspicious-looking free() or delete statements and easily see whether they cause memory leaks.

幼儿园老大 2024-08-27 16:36:16

AFAIK,Windows 中的 Boundscheck 做得非常好。在我的一个项目中,它发现了一些非常奇怪的错误。

AFAIK, Boundscheck in Windows does a very good job. In one of my project, it caught some very weird errors.

月棠 2024-08-27 16:36:16

为了避免在我自己的项目(在 Windows 上)中出现这种情况,我编写了自己的内存分配器,简单地称为 VirtualAlloc 和 VirtualFree。它为每个请求分配一个额外的页面,将其与最后一页的左侧对齐,并在访问最后一页时使用 VirtualProtect 生成异常。这可以当场检测到越界访问,甚至只是读取。

免责声明:我绝不是第一个有这个想法的人。

例如,如果页面为 4096 字节,并且调用了 new int[1],则分配器将:

  1. 分配 8192 字节(需要 4 个字节,即一页,额外的保护页使总数达到 2 页)
  2. 将最后一页标记为不可访问
  3. 确定要返回的地址(最后分配的页从 4096 开始... 4096 - 2 = 4092)

以下代码:

main() {
    int *array = new int[10];
    return array[10];
}

将立即生成访问冲突。

它还有一个(编译时)选项来检测超出分配的左侧的访问(即数组[-1]),但此类错误似乎很少见,所以我从未使用过选项。

To avoid this in my own projects (on Windows), I wrote my own memory allocator which simply called VirtualAlloc and VirtualFree. It allocated an extra page for each request, aligned it just to the left of the last page, and used VirtualProtect to generate an exception whenever the last page was accessed. This detected out-of-bounds accesses, even just reads, on the spot.

Disclaimer: I was by no means the first to have this idea.

For example, if pages are 4096 bytes, and new int[1] was called, the allocator would:

  1. Allocate 8192 bytes (4 bytes are needed, which is one page, and the extra guard page brings the total to 2 pages)
  2. Mark the last page unaccessible
  3. Determine the address to return (the last allocated page starts at 4096... 4096 - 2 = 4092)

The following code:

main() {
    int *array = new int[10];
    return array[10];
}

would then generate an access violation on the spot.

It also had a (compile-time) option to detect accesses beyond the left side of the allocation (ie, array[-1]), but these kinds of errors seemed rare, so I never used the option.

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