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

Segmentation Fault (Core Dumped) [closed]

asked 2016-09-05 09:53:05 -0500

R.Mehra gravatar image

updated 2016-09-05 09:54:44 -0500

Hi all,

I am a new ROS user. I was going through the Publisher and Subscriber tutorials and I attempted to try out the same with an Integer data type. So I made two nodes.

My publisher node :

#include "ros/ros.h"
#include <std_msgs/Int16.h>
int main(int argc, char **argv)
 ros::init(argc, argv, "pub_node");
 ros::NodeHandle p;
 ros::Publisher my_var = p.advertise<std_msgs::Int16>("my_topic", 1000);
 ros::Rate loop_rate(1);
 int count = 0;

 while (ros::ok())
 std_msgs::Int16 dog; = count;
return 0;

My subscriber node :

#include "ros/ros.h"
#include "std_msgs/Int16.h"

void sub_call(const std_msgs::Int16::ConstPtr& msg)
  ROS_INFO("I heard: [%s]", msg->data);

int main(int argc, char **argv)
ros::init(argc, argv, "my_sub");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("my_topic", 1000, sub_call);
return 0;

I am able to build these both. I can run the publisher node and I can see the data being published through

$ rostopic echo /my_topic

The problem is that as soon as I run the subscriber node, it shows

Segmentation fault (core dumped)

and the subscriber node is stopped.

What does this mean?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by R.Mehra
close date 2017-02-04 09:54:34.179188

1 Answer

Sort by ยป oldest newest most voted

answered 2016-09-05 10:59:45 -0500

I think your issue is in the ROS_INFO call inside of your callback. You are trying to print a string value with %s, but your data is an integer value. Try changing the %s to a %d.

This mistake likely throws a compiler warning when you compile your code. Something like

format %s expects argument of type char*, but argument 8 has type int

edit flag offensive delete link more


That was it. I understood the problem and the solution now. Is there any way by which we can check for errors while writing the nodes before running them? AFAIK, building the package doesn't help with error detection, right?

R.Mehra gravatar image R.Mehra  ( 2016-09-06 05:24:15 -0500 )edit

If you read the output of the compilation when building the package you will be notified of compiler warnings and errors. This is how you check before running. It takes practice to understand what the errors mean and how to fix them, but it is definitely one way to catch errors before running.

jarvisschultz gravatar image jarvisschultz  ( 2016-09-08 08:51:58 -0500 )edit

There are also many syntax checkers that can be integrated with an editor or an IDE.

jarvisschultz gravatar image jarvisschultz  ( 2016-09-08 08:54:55 -0500 )edit

Just to add a small note: do consider using GDB to debug if you see segmentation fault. Take a look at this:

DavidN gravatar image DavidN  ( 2016-09-08 23:26:15 -0500 )edit

Question Tools

1 follower


Asked: 2016-09-05 09:53:05 -0500

Seen: 11,072 times

Last updated: Sep 05 '16