# segmentation fault when trying to fill up a msg

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:
//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.

edit retag close merge delete

Sort by » oldest newest most voted

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");


more

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!

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

## Stats

Seen: 1,113 times

Last updated: Sep 19 '13