Boost 在重载 new/delete 运算符时会导致无效块

发布于 2024-10-09 13:35:34 字数 612 浏览 4 评论 0原文

我遇到一个问题,该问题似乎是在 Boost 调用 Boost:runtime:cla::parser::~parser 期间发生的无效内存块。当对该对象调用全局删除时,C++ 断言内存块无效:

dbgdel.cpp(52):

/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

我所做的一项调查显示,问题的发生是由于 new/delete 运算符的全局重载造成的。这些重载被放置在单独的 DLL 中。我发现只有当 DLL 在 RELEASE 中编译而主应用程序在 DEBUG 中编译时才会出现问题。

所以我认为,在重载 new/delete 运算符时,Release/Debug 构建风格可能会在 Boost/CRT 中产生这样的问题。因此,即使在释放模式下,我也尝试使用重载函数显式调用 _malloc_dbg 和 _free_dbg ,但它没有解决无效堆块问题。

知道问题的根本原因是什么吗?这种情况可以解决吗?

我应该强调的是,这个问题是在我开始使用 Boost 时才出现的。在此之前,CRT 从未抱怨过任何无效内存块。那么这可能是 Boost 内部的错误吗?

谢谢!

I have a problem which appears to a be an invalid memory block that happens during a Boost call to Boost:runtime:cla::parser::~parser. When that global delete is called on that object, C++ asserts on the memory block as an invalid:

dbgdel.cpp(52):

/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

An investigation I did revealed that the problem happened because of a global overloading of the new/delete operators. Those overloadings are placed in a separate DLL. I discovered that the problem happens only when that DLL is compiled in RELEASE while the main application is compiled in DEBUG.

So I thought that the Release/Debug build flavors might have created a problem like this in Boost/CRT when overloading new/delete operators. So I then tried to explicitly call to _malloc_dbg and _free_dbg withing the overloading functions even in release mode, but it didn't solve the invalid heap block problem.

Any idea what the root cause of the problem is? is that situation solvable?

I should stress that the problem began only when I started to use Boost. Before that CRT never complained about any invalid memory block. So could it be an internal Boost bug?

Thanks!

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

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

发布评论

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

评论(1

热鲨 2024-10-16 13:35:34

我发现只有当 DLL 在 RELEASE 中编译而主应用程序在 DEBUG 中编译时才会出现问题

一般来说,如果一种运行时类型的内存分配被另一种类型释放,则不能将使用发布 CRT 的模块与调试 CRT 混合使用运行时(可能还有其他问题,但内存分配问题似乎是最常见的)。

解决方案是使用针对调试运行时构建的模块,以及针对发布运行时构建的模块 - 不要混合两者。

微软的解释可以在这里找到:http://msdn.microsoft.com/en-我们/library/ms235460.aspx

由于 CRT 库的每个副本都有自己的堆管理器,因此在一个 CRT 库中分配内存并将指针跨过 DLL 边界传递以由 CRT 库的不同副本释放,是导致堆损坏的潜在原因。< /p>

I discovered that the problem happens only when that DLL is compiled in RELEASE while the main application is compiled in DEBUG

In general you cannot mix modules that use the release CRT with the debug CRT if memory allocation from one type of runtime is freed by the other runtime (there might be other issues, but memory allocation problems seem to crop up the most).

The solution is to use modules that are built against the debug runtime with each other, and modules that are built against the release runtime with each other - don't mix the two.

Microsoft's explanation can be found here: http://msdn.microsoft.com/en-us/library/ms235460.aspx

because each copy of the CRT library has its own heap manager, allocating memory in one CRT library and passing the pointer across a DLL boundary to be freed by a different copy of the CRT library is a potential cause for heap corruption.

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