0
$\begingroup$

Rosanswers logo

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.


Originally posted by farhansajjad on ROS Answers with karma: 35 on 2016-12-17

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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


Originally posted by ahendrix with karma: 47576 on 2016-12-18

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$