ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

segmentation fault when trying to fill up a msg

asked 2013-09-19 06:48:48 -0600

E1000ii gravatar image

I'm building a JointState publisher for my custom Robot. Its almost all done. But when i try to fill the message i get a segmentation fault. I was investigating it for hours, but found nothing that seems to be wrong. Either i had not been able to debut the node with gdb.

Actually this is the code that crashes.

//create a JointState message
sensor_msgs::JointState msg = sensor_msgs::JointState();
//doing that end up in a seg fault:
msg.header.stamp = ros::Time::now();
//doing that also up in a seg fault:
msg.name[0] = "joint1";

I had not compilation errors or warnings, added to sensor_msgs as dependences in package.xml and CMakeLists.txt.

Maibe its a silly error and i was to many hour working on that for today. Hope someone could point me to fix it.

Thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2013-09-19 07:18:24 -0600

The problem is that the msg.name field is defined in the message definition as string[] name. According to this documentation, messages with variable length arrays get treated as C++ std::vector datatypes. So when you instantiate a new message in your second line, the size of the name field's std::vector is not set. Thus, using msg.name[0] throws a seg fault because you are trying to access unallocated memory. So you either need to declare the size of the vector before trying to access the elements, or use dynamic allocating methods like std::vector::push_back. So you could for example change the last line to

msg.name.push_back("joint1");

See this page for more information about the vector class.

edit flag offensive delete link more

Comments

1

Yep, youre right. I solved the array using them as a vector. I just missed that docs... but i still get the segfaulg when adding the time stamp. Not a big deal now. I will investigate it further. Thanks again man!

E1000ii gravatar image E1000ii  ( 2013-09-19 22:08:48 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-09-19 06:48:48 -0600

Seen: 1,962 times

Last updated: Sep 19 '13