对GCC / Windows DLLs / C ++ STL前面的任何改进? [英] Any improvements on the GCC/Windows DLLs/C++ STL front?

查看:236
本文介绍了对GCC / Windows DLLs / C ++ STL前面的任何改进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,当使用Cygwin下使用GCC编译的DLL时,我遇到了一个很讨厌的崩溃。基本上,只要你运行一个调试器,你可能最终着陆在一个调试陷阱引起的RtlFreeHeap()接收一个地址,它没有分配。



这是一个针对Cygwin的 GCC 3.4 已知错误。出现这种情况是因为libstdc ++库包括对空字符串的聪明优化。我给你的详细信息(参见整篇文章的参考文献),但是每当你在一个DLL中为一个属于另一个DLL的std :: string对象分配内存时,你最终会给一个堆释放一个块另一个堆。因此,在RtlFreeHeap()中 SIGTRAP



当DLL异常被跨越DLL边界时,报告有其他问题。



这使得GCC 3.4在Windows上是一个不可接受的解决方案,一旦你的项目是基于DLL和STL。我有几个选项可以通过此选项,其中许多非常耗时和/或烦人:





我还不能切换到另一个编译器,因为我使用的一些其他工具。



没有人可以告诉我们,这个问题可能不是一个问题,有一些新闻吗?我找不到任何明确的通知,这已被修正(错误仍然标记为已分配),除了一个评论 GNU无线电错误追踪器



谢谢!

解决方案

你遇到的一般问题是C ++从来不是一个组件语言。它真的设计用于创建完整的独立应用程序。像共享库和其他这样的机制的事情是由供应商自己创建的。想想这个例子:假设你创建了一个返回C ++对象的C ++组件。 C ++组件如何知道它将被C ++调用者使用?如果调用者是一个C ++应用程序,为什么不直接使用库?



当然,上面的信息并不能真正帮助你。



相反,我将创建共享库/ DLL,以便遵循几个规则:



  1. 当所有创建的对象被销毁时,可以安全地卸载它。

您可能需要在组件中创建额外的API以确保这些规则,但是通过遵循这些规则,它将确保不会发生像所描述的那些问题。 / p>

Yesterday, I got bit by a rather annoying crash when using DLLs compiled with GCC under Cygwin. Basically, as soon as you run with a debugger, you may end up landing in a debugging trap caused by RtlFreeHeap() receiving an address to something it did not allocate.

This is a known bug with GCC 3.4 on Cygwin. The situation arises because the libstdc++ library includes a "clever" optimization for empty strings. I spare you the details (see the references throughout this post), but whenever you allocate memory in one DLL for an std::string object that "belongs" to another DLL, you end up giving one heap a chunk to free that came from another heap. Hence the SIGTRAP in RtlFreeHeap().

There are other problems reported when exceptions are thrown across DLL boundaries.

This makes GCC 3.4 on Windows an unacceptable solution as soon as your project is based on DLLs and the STL. I have a few options to move past this option, many of which are very time-consuming and/or annoying:

I cannot (yet) switch to another compiler either, because of some other tools I'm using. The comments I find from some GCC people is that "it's almost never reported, so it's probably not a problem", which annoys me even more.

Does anyone have some news about this? I can't find any clear announcement that this has been fixed (the bug is still marked as "assigned"), except one comment on the GNU Radio bug tracker.

Thanks!

解决方案

The general problem you're running into is that C++ was never really meant as a component language. It was really designed to be used to create complete standalone applications. Things like shared libraries and other such mechanisms were created by vendors on their own. Think of this example: suppose you created a C++ component that returns a C++ object. How is the C++ component know that it will be used by a C++ caller? And if the caller is a C++ application, why not just use the library directly?

Of course, the above information doesn't really help you.

Instead, I would create the shared libraries/DLLs such that you follow a couple of rules:

  1. Any object created by a component is also destroyed by the same component.
  2. A component can be safely unloaded when all of its created objects are destroyed.

You may have to create additional APIs in your component to ensure these rules, but by following these rules, it will ensure that problems like the one described won't happen.

这篇关于对GCC / Windows DLLs / C ++ STL前面的任何改进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆