2

我正在用 g++ 构建,昨天一个对 SO 有帮助的人告诉我用-D_GLIBCXX_DEBUGand-D_GLIBCXX_DEBUG_PEDANTIC标志编译。我这样做了,而且我昨天大部分时间都在调整我的代码以符合这些标志。现在它在抱怨我对 的使用boost::signal,我不确定问题出在哪里。

我有一个类Yarl,它有一个refresh()我想绑定到sigRefresh另一个类中的信号的函数EventHandler

class Yarl
{
    private:
        void refresh();
    (...)
};

class EventHandler
{
    public:
        boost::signal<void()> sigRefresh;
    (...)
}

然后,在 的成员函数中Yarl,我有这段代码:

EventHandler eventHandler;
eventHandler.sigRefresh.connect(boost::bind(&Yarl::refresh, this));

在我开始使用这些标志进行编译之前,这段代码运行良好。现在我正在使用它们,我的程序在第二行出现段错误。

这是来自 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

有人看到我应该修复什么吗?

编辑:我有一个说明问题的小示例程序,正如 Daniel Trebbien 所建议的那样。

这是头文件(test.hpp):

#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();
}

和以前一样,这个示例程序在 g++ 中编译时工作正常,只有一个-g标志,但如果我添加-D_GLIBCXX_DEBUGand -D_GLIBCXX_DEBUG_PEDANTIC,它会出现段错误eventHandler.sigRefresh.connect


-D_GLIBCXX_DEBUG我用and重新编译了 boost -D_GLIBCXX_DEBUG_PEDANTIC,它并没有解决问题,但是在编译时我注意到它做了一些奇怪的事情。我使用这个命令用 bjam 编译(根据这个boost 教程):

sudo bjam --build-dir=. --toolset=gcc --variant=debug --cxxflags=-D_GLIBCXX_DEBUG,-D_GLIBCXX_DEBUG_PEDANTIC --layout=tagged stage

尽管有--variant=debug标签,它仍在编译发布版本。我也没有在输出的任何地方看到我的调试标志。有没有可能我编译错了?

4

1 回答 1

1

我是否必须为发布代码和调试代码使用不同编译版本的 boost?

恐怕你会。从个人经验来看,boost对编译器标志的变化极为敏感。几年前,我正在破解的一个免费软件项目不得不停止使用boost::systemboost::filesystem只是因为这些模块具有共享库,这些库没有可靠地编译(由 Linux 发行商),其标志与我们的代码完全相同。症状是一样的——在正确的代码上莫名其妙的崩溃。

因此,我不得不建议不要使用任何附带共享库的 Boost 模块。曾经。这是可悲的。

于 2010-07-11T07:05:58.037 回答