Segmentation fault in returning array from a class
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.