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

Segmentation fault in returning array from a class

asked 2016-12-17 15:32:53 -0600

farhansajjad gravatar image

updated 2016-12-17 16:45:45 -0600

I'm facing a strange problem. I'm running the ROS node on Intel Edison and in the node I'm reading the Bluetooth LE device MAC address and its RSSI value from a class which I've written in an external file. All functions are working correctly except one. When I try to get back the data from scan.read_decvice function it runs correctly for the first time but in the next iteration I get a segmentation fault.

while (ros::ok())
{
    bl_talker::beacon le_msg;
    dev_info devices[20];
    int num_dev = scan.read_device(device, 20, devices);
    while(num_dev > 0)
    {
        if(devices[num_dev-1].valid == 1)
        {
            le_msg.mac = devices[num_dev-1].dev_mac;
            le_msg.rssi = devices[num_dev-1].dev_rssi;
            le_msg.msg_count = devices[num_dev-1].dev_count;

            printf("%d: %s - RSSI %d\n", le_msg.msg_count, le_msg.mac.c_str(), le_msg.rssi);
            chatter_pub.publish(le_msg);
            num_dev--;
        }
    }
    ros::spinOnce();
    loop_rate.sleep();
}

If I write the function in the same file where my main is written, it runs without any problem. Also it runs smoothly if I remove the ROS part. It would be great if anyone can help me out.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-12-17 23:12:16 -0600

ahendrix gravatar image

Sounds like a pretty typical memory corruption bug. You're probably reading or writing past the end of an array somewhere.

The obvious thing that I notice is that you're not bounds-checking num_dev, but the real solution here is to run your node in a memory checker like valgrind. That will check your memory accesses and point out which function and which line is writing where it shouldn't.

If you're running your node with rosrun, you can use the --prefix option to rosrun or if you're running your node from a launch file, you can follow Roslaunch Nodes in Valgrind or GDB

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-12-17 15:32:53 -0600

Seen: 170 times

Last updated: Dec 17 '16