ros::spin() Segmentation fault
I got a coredump about ros:spin(), anyone tell me how to fix it, thanks
gdb coredump info:
#0 0x000000000056a89c in boost::detail::sp_counted_base::release (
this=0x7f68009050)
at /opt/allwinner/rootfs/staging_dir/target/usr/include/boost/smart_ptr/detail/sp_counted_base_std_atomic.hpp:109
#1 0x000000000056a950 in boost::detail::shared_count::~shared_count (
this=0x7ffa6b1ea8, __in_chrg=<optimized out>)
at /opt/allwinner/rootfs/staging_dir/target/usr/include/boost/smart_ptr/detail/shared_count.hpp:419
#2 0x0000007f9b158270 in ros::MultiThreadedSpinner::spin(ros::CallbackQueue*)
() from /usr/lib/libroscpp.so
#3 0x0000007f9b15538c in std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::operator=(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) () from /usr/lib/libroscpp.so
#4 0x0000007f9b124f78 in boost::enable_shared_from_this<ros::PublisherLink>::shared_from_this() () from /usr/lib/libroscpp.so
#5 0x0000007f9b1255e4 in std::deque<ros::CallbackQueue::CallbackInfo, std::allocator<ros::CallbackQueue::CallbackInfo> >::_M_destroy_data_aux(std::_Deque_iterator<ros::CallbackQueue::CallbackInfo, ros::CallbackQueue::CallbackInfo&, ros::CallbackQueue::CallbackInfo*>, std::_Deque_iterator<ros::CallbackQueue::CallbackInfo, ros::CallbackQueue::CallbackInfo&, ros::CallbackQueue::CallbackInfo*>) [clone .isra.106] () from /usr/lib/libroscpp.so
#6 0x0000007f9b156f54 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) [clone .isra.89] () from /usr/lib/libroscpp.so
#7 0x0000007f9b1475dc in void std::vector<boost::shared_ptr<rosgraph_msgs::Log_<std::allocator<void> > >, std::allocator<boost::shared_ptr<rosgraph_msgs::Log_<std::allocator<void> > > > >::_M_emplace_back_aux<boost::shared_ptr<rosgraph_msgs::Log_<std::allocator<void> > > const&>(boost::shared_ptr<rosgraph_msgs::Log_<std::allocator<void> > > const&) () from /usr/lib/libroscpp.so
#8 ros::spin()
code like follow:
void chatterCallback(const std_msgs::Int16ConstPtr & msg){
// do something
}
int main(){
ros::init(argc, argv, "test");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::Int16>("chatter_A", 1000);
ros::Subscriber sub = n.subscribe("chatter_B", 1000, chatterCallback);
std::thread([&](){
while(1){
std_msgs::Int16 a;
a.data = 16;
chatter_pub.publish(a);
sleep(1);
}
});
ros::spin();
}
Asked by robtos on 2023-07-02 23:51:19 UTC
Answers
The biggest problem with your code is that you are not using std::thread correctly. I believe you need to declare an object name for the thread like this:
std::thread t1(func_or_lambda);
Another issue is that your background thread does not call ros::ok()
to make sure that it is safe to call chatter_pub.publish(a). You may get a crash if you call publish() after ros has shut down. The tutorial link below shows one way to use ros::ok().
If you are not familiar with c++ and c++ multi-threaded programming, you are asking for trouble using threads. You can easily create race conditions with very difficult-to-find bugs. My advice is to start out using the style shown in the Pub/Sub tutorial until you get a better feel for how ros is supposed to behave. You are more likely to get the behavior you want, and your ros node is less likely to create a crash.
Asked by Mike Scheutzow on 2023-07-07 07:17:44 UTC
Comments
It is most likely that you have a bug in the source code for this ros node. If you want help, you need to show us that src code. Either 1) copy/paste it into the description and format with 101010 button, or 2) give us a url to the .cpp file in a github.com repository.
You can edit your description by using the "edit" button near the end of the description.
Asked by Mike Scheutzow on 2023-07-03 07:50:49 UTC
thanks; I have add likely code near the end of the description, i'm sorry my bad english, and code style;
Asked by robtos on 2023-07-06 01:10:28 UTC