gpt4 book ai didi

c++ - Boost::signal 内存访问错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:57 25 4
gpt4 key购买 nike

我正在尝试使用 boost::signal 来实现回调机制,并且我在 boost::signal 代码中获得了内存访问断言,即使是对库的最微不足道的使用。我已将其简化为以下代码:

#include <boost/signal.hpp>

typedef boost::signal<void (void)> Event;

int main(int argc, char* argv[])
{

Event e;

return 0;
}

谢谢!

编辑:这是使用带有 SP1 的 Visual Studio 2008 编译的 Boost 1.36.0。 Boost::filesystem 和 boost::signal 一样也有一个必须链接进去的库,看起来工作正常。我相信我使用的所有其他 boost 库都只有 header 。

最佳答案

我已经确认这是一个问题 - Stephan T Lavavej (STL!) 在 Microsoft blogged about this .

具体来说,他说:

The general problem is that the linker does not diagnose all One Definition Rule (ODR) violations. Although not impossible, this is a difficult problem to solve, which is why the Standard specifically permits certain ODR violations to go undiagnosed.

I would certainly love for the compiler and linker to have a special mode that would catch all ODR violations at build time, but I recognize that that would be difficult to achieve (and would consume resources that could perhaps be put to even better use, like more conformance). In any event, ODR violations can be avoided without extreme effort by properly structuring your code, so we as programmers can cope with this lack of linker checking.

However, macros that change the functionality of code by being switched on and off are flirting dangerously with the ODR, and the specific problem is that _SECURE_SCL and _HAS_ITERATOR_DEBUGGING both do exactly this. At first glance, this might not seem so bad, since you should already have control over which macros are defined project-wide in your build system. However, separately compiled libraries complicate things - if you've built (for example) Boost with _SECURE_SCL on, which is the default, your project must not turn _SECURE_SCL off. If you're intent on turning _SECURE_SCL off in your project, now you have to re-build Boost accordingly. And depending on the separately compiled library in question, that might be difficult (with Boost, according to my understanding, it can be done, I've just never figured out how).

他稍后在评论中列出了一些可能的解决方法,但没有一个看起来适合这种情况。其他人报告说,通过在 boost/config/compiler/visualc.hpp 中插入一些定义,可以在编译 boost 时关闭这些标志,但这对我来说无效。但是,在 tools/build/v2/user-config.jam 中插入以下行 VERBATIM 就可以了。请注意,空格对于增强卡纸很重要。

using msvc : 9.0 : : <cxxflags>-D _SECURE_SCL=0 <cxxflags>-D _HAS_ITERATOR_DEBUGGING=0 ;

关于c++ - Boost::signal 内存访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/137089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com