0

I created a new controller for Webots in Visual Studio in c ++ (Wizard -> new project controller ...) but when I run the simulation the controller is crashed

this is the code: Basically it is the controller that comes from deafult

#include <webots/DistanceSensor.hpp>
#include <webots/Robot.hpp>
using namespace webots;

int main(int argc, char **argv) {
 Robot *robot = new Robot();
  int timeStep = (int)robot->getBasicTimeStep();
   DistanceSensor *ds = robot->getDistanceSensor("ds_left");
   ds->enable(timeStep);

 while (robot->step(timeStep) != -1) {

  double val = ds->getValue();
 };


  delete robot;
  return 0;
}

After debugging Visual Studio show following WARNING:

Severity    Code    Description Project File    Line    Suppression State
Warning C26451  Arithmetic overflow: Using operator '+' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '+' to avoid overflow (io.2). Prueba7 C:\PROGRAM FILES\WEBOTS\RESOURCES\LANGUAGES\CPP\ROBOT.CPP   465 

And when i tried to use the controller in webots, it shows:

WARNING: Prueba7: The process crashed some time after starting successfully.
WARNING: 'Prueba7' controller crashed.
user4581301
  • 29,019
  • 5
  • 26
  • 45
  • Notes: You may find the error messages in the build output provided buy the Output tab more useful than the Error List. They're more complete and easier to cut and paste as text. [Avoid using `new` unless forced to.](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) Nothing here seems to be forcing you to, so you can save your program a lot of work and potential errors. – user4581301 Aug 08 '19 at 22:17
  • If I'm reading the error message right, it's talking about a problem on line 465. I'm not seeing 465 lines here. – user4581301 Aug 08 '19 at 22:19
  • deviceList.resize(count + 1); is the line 465 of the ROBOT.CPP however I cannot modify it because it belongs to Webots and places it in the project automatically when the controller is created – Tomas Guevara Aug 08 '19 at 22:26
  • OK. that warning is kinda bogus in this case. The problem the compiler is warning about is if the two numbers being added are large enough, you could overflow a 32 bit number even though `resize` accepts a 64 bit number. `count + 1`'s not going to cause that to happen in any reasonable use case. I've shooting blind here, but It might be worth seeing if you can change the compilation to produce a 32 bit executable to see what happens. – user4581301 Aug 08 '19 at 22:37

0 Answers0