Robotics StackExchange | Archived questions

Segmentation fault (core dumped)

Hello dears, i would like to simply scan front but i got this error "Segmentation fault (core dumped)" do you know what is the reason or how to fix this? thank you.


double FrontRange(sensor_msgs::LaserScan & LaserScanMsg) {

return LaserScanMsg.ranges[LaserScanMsg.ranges.size()/2]; }

Asked by arya on 2018-07-10 07:59:01 UTC

Comments

You should print the size of the range before the return to check if the message isn't empty. Maybe you are calling your function too early so the first message hasn't been received yet, so you are sending an empty message (leading to the segfault issue).

Asked by Delb on 2018-07-10 09:40:28 UTC

Moreover, are you sure the issue comes from this function in particular ?

Asked by Delb on 2018-07-10 09:40:47 UTC

Answers

You can first try to check if the message is empty by using LaserScanMsg.ranges.empty(). If this returns a 1, then your message is indeed empty, and any other action you try to do on it will result in a Segmentation Fault.

I would suggest the following:

if( !LaserScanMsg.ranges.empty() ) { return LaserScanMsg.ranges[LaserScanMsg.ranges.size()/2]; }

Asked by Infection on 2018-07-18 03:59:15 UTC

Comments