Boost: what could be the reasons for a crash in boost::slot<>::~slot?

人盡茶涼 提交于 2020-01-17 04:32:06

问题


I am getting such a crash:

#0  0x90b05955 in __gnu_debug::_Safe_iterator_base::_M_detach
#1  0x90b059ce in __gnu_debug::_Safe_iterator_base::_M_attach
#2  0x90b05afa in __gnu_debug::_Safe_sequence_base::_M_detach_all
#3  0x000bc54f in __gnu_debug::_Safe_sequence_base::~_Safe_sequence_base at safe_base.h:170
#4  0x000aac05 in __gnu_debug::_Safe_sequence<__gnu_debug_def::vector<boost::signals::trackable const*, std::allocator<boost::signals::trackable const*> > >::~_Safe_sequence at safe_sequence.h:97
#5  0x000ac9c1 in __gnu_debug_def::vector<boost::signals::trackable const*, std::allocator<boost::signals::trackable const*> >::~vector at vector:95
#6  0x000acf65 in boost::signals::detail::slot_base::data_t::~data_t at slot.hpp:32
#7  0x000acf8f in boost::checked_delete<boost::signals::detail::slot_base::data_t> at checked_delete.hpp:34
#8  0x000b081e in boost::detail::sp_counted_impl_p<boost::signals::detail::slot_base::data_t>::dispose at sp_counted_impl.hpp:78
#9  0x0000a016 in boost::detail::sp_counted_base::release at sp_counted_base_gcc_x86.hpp:145
#10 0x0000a046 in boost::detail::shared_count::~shared_count at shared_count.hpp:217
#11 0x000a9fb0 in boost::shared_ptr<boost::signals::detail::slot_base::data_t>::~shared_ptr at shared_ptr.hpp:169
#12 0x000aa459 in boost::signals::detail::slot_base::~slot_base at slot.hpp:27
#13 0x000aad07 in boost::slot<boost::function<bool ()(char, int)> >::~slot at slot.hpp:105
#14 0x001b943b in main at vermes.cpp:102

This is the code:

#include <boost/signal.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>

bool dummyfunc(char,int) { return false; }

int main(int argc, char **argv)
{   
    boost::signal<bool (char, int)> myslot;
    myslot.connect(0, &dummyfunc);
    return 0;
}

It's the first time I am working with Boost and I am also completly new to the code of the project I am trying to port here.

That is why I would like to ask if such a crash could be in any way explained by Boost or if it must be unrelated to Boost.

I already tried to understand the crash itself but I got stuck somehow. It seems that probably the std::vector, which is going to be deleted here, is messed up (messed up = memory corrupt). That vector is a member of slot_base::data_t. The deletion is done in the destructor of slot_base::shared_ptr. So perhaps the shared_ptr also was messed up - so perhaps even the whole slot_base was messed up. But in the code I have, I don't really see a reason why that memory could be messed up. It is even the first access at all after the construction of myslot.

Addition: What I also don't really understand is why the ~slot_base() is called here at all when I do the connect. But I also didn't found the connect-memberfunction. Is that a magic makro somewhere?


回答1:


I found the problem. When I enable these preprocessor definitions (my Xcode does that by default in Debug configuration), it crashes:

-D _GLIBCXX_DEBUG=1
-D _GLIBCXX_DEBUG_PEDANTIC=1

I guess Boost (bjam) compiled without those and that causes such problems because the STL structures (like vector) look different in binary form when compiled with or without this.




回答2:


It sounds like your GConsole class is not derived from boost::trackable.

When a signal is bound to a member function, it expects the member's object to exist, always.

You can either explicitly disconnect signals when member function's owner is destroyed, or you can derive the object from boost::trackable, which will do the maintenance automatically when the object is destroyed.



来源:https://stackoverflow.com/questions/1823605/boost-what-could-be-the-reasons-for-a-crash-in-boostslotslot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!