Ask Your Question
0

Segmentation Fault (Core Dumped) [closed]

asked 2016-09-05 09:53:05 -0600

R.Mehra gravatar image

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

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)
{
 //Initialization
 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;

 //loop
 while (ros::ok())
 {
 std_msgs::Int16 dog;
 dog.data = count;
 my_var.publish(dog);
 ros::spinOnce();
 loop_rate.sleep();
 ++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);
ros::spin();
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
0

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

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

Comments

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 imageR.Mehra ( 2016-09-06 05:24:15 -0600 )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 imagejarvisschultz ( 2016-09-08 08:51:58 -0600 )edit

There are also many syntax checkers that can be integrated with an editor or an IDE. https://en.wikipedia.org/wiki/List_of...http://www.flycheck.org/https://github.com/scrooloose/syntastic

jarvisschultz gravatar imagejarvisschultz ( 2016-09-08 08:54:55 -0600 )edit
1

Just to add a small note: do consider using GDB to debug if you see segmentation fault. Take a look at this: http://wiki.ros.org/roslaunch/Tutoria...

DavidN gravatar imageDavidN ( 2016-09-08 23:26:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-09-05 09:53:05 -0600

Seen: 5,583 times

Last updated: Sep 05 '16