C++ 使用带-D_GLIBCXX_调试编译器标志的boost::signal时出现SEGFULT
我正在使用g++进行构建,昨天,一位非常有用的人告诉我使用C++ 使用带-D_GLIBCXX_调试编译器标志的boost::signal时出现SEGFULT,c++,boost-signals,segmentation-fault,C++,Boost Signals,Segmentation Fault,我正在使用g++进行构建,昨天,一位非常有用的人告诉我使用-D_GLIBCXX_DEBUG和-D_GLIBCXX_DEBUG_PEDANTIC标志进行编译。我这样做了,昨天的大部分时间我都在调整代码以符合这些标志。现在它抱怨我使用了boost::signal,我不确定问题出在哪里 我有一个类Yarl,它有一个函数refresh(),我想绑定到另一个类EventHandler中的信号sigfresh: class Yarl { private: void refresh()
-D_GLIBCXX_DEBUG
和-D_GLIBCXX_DEBUG_PEDANTIC
标志进行编译。我这样做了,昨天的大部分时间我都在调整代码以符合这些标志。现在它抱怨我使用了boost::signal
,我不确定问题出在哪里
我有一个类Yarl
,它有一个函数refresh()
,我想绑定到另一个类EventHandler
中的信号sigfresh
:
class Yarl
{
private:
void refresh();
(...)
};
class EventHandler
{
public:
boost::signal<void()> sigRefresh;
(...)
}
在开始使用这些标志编译之前,这段代码运行良好。现在我正在使用它们,我的程序在第二行出现故障
以下是来自gdb的回溯:
#0 0x001eeee6 in __gnu_debug::_Safe_iterator_base::_M_detach_single() ()
from /usr/lib/libstdc++.so.6
#1 0x001f0555 in __gnu_debug::_Safe_sequence_base::_M_detach_all() ()
from /usr/lib/libstdc++.so.6
#2 0x0804e8a3 in ~_Safe_sequence_base (this=0x812cda4,
__in_chrg=<value optimized out>)
at /usr/include/c++/4.4/debug/safe_base.h:180
#3 0x08085af9 in __gnu_debug::_Safe_sequence<std::__debug::vector<boost::signals::trackable const*, std::allocator<boost::signals::trackable const*> > >::~_Safe_sequence() ()
#4 0x08085b44 in std::__debug::vector<boost::signals::trackable const*, std::allocator<boost::signals::trackable const*> >::~vector() ()
#5 0x080873ab in boost::signals::detail::slot_base::data_t::~data_t() ()
#6 0x080873e3 in void boost::checked_delete<boost::signals::detail::slot_base::data_t>(boost::signals::detail::slot_base::data_t*) ()
#7 0x0808802e in boost::detail::sp_counted_impl_p<boost::signals::detail::slot_base::data_t>::dispose() ()
#8 0x08083d04 in boost::detail::sp_counted_base::release (this=0x812ce30)
at /usr/local/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:145
#9 0x08083d76 in ~shared_count (this=0xbffff358,
__in_chrg=<value optimized out>)
at /usr/local/boost/smart_ptr/detail/shared_count.hpp:217
#10 0x08083f70 in ~shared_ptr (this=0xbffff354,
__in_chrg=<value optimized out>)
at /usr/local/boost/smart_ptr/shared_ptr.hpp:169
#11 0x080847f1 in ~slot_base (this=0xbffff354, __in_chrg=<value optimized out>)
at /usr/local/boost/signals/slot.hpp:27
#12 0x08084829 in ~slot (this=0xbffff354, __in_chrg=<value optimized out>)
at /usr/local/boost/signals/slot.hpp:105
#13 0x0808390f in yarl::Yarl::mainLoop (this=0xbffff3dc) at src/Yarl.cpp:408
#14 0x08083a96 in yarl::Yarl::startGame (this=0xbffff3dc) at src/Yarl.cpp:452
#15 0x08083abe in main () at src/Yarl.cpp:461
尽管有--variant=debug
标记,它仍然在编译发布版本。我也没有在输出中看到任何关于调试标志的内容。可能是我编错了吗
对于发布代码和调试代码,我是否必须有不同的boost编译版本
恐怕是的。根据个人经验,boost对编译器标志的更改非常敏感。几年前,我参与的一个自由软件项目不得不停止使用boost::system
和boost::filesystem
,这仅仅是因为这些模块共享的库(由Linux发行商)没有使用与我们的代码完全相同的标志进行可靠编译。症状是一样的——在正确的代码上莫名其妙的崩溃
因此,我建议不要使用任何附带共享库的Boost模块。曾经很遗憾。有没有办法发布一个最小的测试用例?即使您不能将一个最小的测试用例发布到一个公共网站上,创建一个测试用例有时也是非常有帮助的,因为您通常可以更好地了解出了什么问题。尝试从一个只执行
EventHandler事件处理程序的main
方法开始;connect(boost::bind(&Yarl::refresh,this))代码>。如果这不会导致seg。错误,然后迭代地添加代码,构建到原始程序,直到程序崩溃。好的,我会尝试,尽管我可能会有一段时间无法发布它。您是否使用这些标志编译了boost?所有程序组件都应该使用相同的编译器标志进行编译。不,我没有。这可能是问题所在,但这些标志会降低执行速度并使程序变大,因此如果不需要的话,我不想用它们编译boost。对于发布代码和调试代码,我必须有不同编译版本的boost吗?看来我还有更多的编译要做。我暂时无法完成,但我会看看是否能修复它。嗯,我终于能够重新编译了。使用这些标签编译boost没有任何帮助,但是,我在做的时候注意到了一些奇怪的事情。我编辑了我的问题以进一步解释。恐怕我对比亚姆一无所知,只是很多人对它有奇怪的问题。您可以尝试将此问题带到boost build或boost users邮件列表-请参阅
#0 0x001eeee6 in __gnu_debug::_Safe_iterator_base::_M_detach_single() ()
from /usr/lib/libstdc++.so.6
#1 0x001f0555 in __gnu_debug::_Safe_sequence_base::_M_detach_all() ()
from /usr/lib/libstdc++.so.6
#2 0x0804e8a3 in ~_Safe_sequence_base (this=0x812cda4,
__in_chrg=<value optimized out>)
at /usr/include/c++/4.4/debug/safe_base.h:180
#3 0x08085af9 in __gnu_debug::_Safe_sequence<std::__debug::vector<boost::signals::trackable const*, std::allocator<boost::signals::trackable const*> > >::~_Safe_sequence() ()
#4 0x08085b44 in std::__debug::vector<boost::signals::trackable const*, std::allocator<boost::signals::trackable const*> >::~vector() ()
#5 0x080873ab in boost::signals::detail::slot_base::data_t::~data_t() ()
#6 0x080873e3 in void boost::checked_delete<boost::signals::detail::slot_base::data_t>(boost::signals::detail::slot_base::data_t*) ()
#7 0x0808802e in boost::detail::sp_counted_impl_p<boost::signals::detail::slot_base::data_t>::dispose() ()
#8 0x08083d04 in boost::detail::sp_counted_base::release (this=0x812ce30)
at /usr/local/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:145
#9 0x08083d76 in ~shared_count (this=0xbffff358,
__in_chrg=<value optimized out>)
at /usr/local/boost/smart_ptr/detail/shared_count.hpp:217
#10 0x08083f70 in ~shared_ptr (this=0xbffff354,
__in_chrg=<value optimized out>)
at /usr/local/boost/smart_ptr/shared_ptr.hpp:169
#11 0x080847f1 in ~slot_base (this=0xbffff354, __in_chrg=<value optimized out>)
at /usr/local/boost/signals/slot.hpp:27
#12 0x08084829 in ~slot (this=0xbffff354, __in_chrg=<value optimized out>)
at /usr/local/boost/signals/slot.hpp:105
#13 0x0808390f in yarl::Yarl::mainLoop (this=0xbffff3dc) at src/Yarl.cpp:408
#14 0x08083a96 in yarl::Yarl::startGame (this=0xbffff3dc) at src/Yarl.cpp:452
#15 0x08083abe in main () at src/Yarl.cpp:461
#include <boost/bind.hpp>
#include <boost/signal.hpp>
#include <iostream>
#include <tr1/memory>
namespace yarl
{
class Yarl
{
private:
void refresh();
public:
void hookSignal();
};
namespace events
{
class EventHandler
{
public:
boost::signal<void()> sigRefresh;
};
}
}
#include "test.hpp"
using namespace std;
namespace yarl
{
void Yarl::refresh()
{
cout << "in refresh" << endl;
}
void Yarl::hookSignal()
{
events::EventHandler eventHandler;
eventHandler.sigRefresh.connect(boost::bind(&Yarl::refresh, this));
eventHandler.sigRefresh();
}
}
int main()
{
yarl::Yarl y;
y.hookSignal();
}
sudo bjam --build-dir=. --toolset=gcc --variant=debug --cxxflags=-D_GLIBCXX_DEBUG,-D_GLIBCXX_DEBUG_PEDANTIC --layout=tagged stage