Exception thrown while processing service call: basic_string::_M_create or std::bad_alloc
Hi everyone,
I have written a c++ node, which provides 2 different services which can be called one after another by a GUI. However the first service call works just fine. When I call the second service on of the two following error messages occur:
It is either:
Exception thrown while processing service call: basic_string::_M_create
or
Exception thrown while processing service call: std::bad_alloc
The callback function is quite simple and shall just execute on function using pointers and then return an int with the value of 1 as a response. The code looks like this:
bool callback(mybot::mysrv::Request &req, mybot::mysrv::Response &res){
ros::AsyncSpinner spinner(10);
spinner.start();
ROS_INFO("callback recieved");
move_point->execute(*current_plan);
res.status_res = 1;
spinner.stop();
return true;
}
The srv file contains:
int8 status_req
---
int8 status_res
EDIT:
If I delete the line move_point->execute(*current_plan); then the call works and just prints the ROS_INFO.
Seems to be a problem using the pointers then. However in the callback of the first service I use the line
move_point->execute(*current_plan);
and it works there, but not in the second function.
Can you explain why you start an
AsyncSpinner
in your callback? This suggests that you either expect or have encountered unexpected or undesired behaviour with your callbacks or other events, but usingAsyncSpinner
like that is really not a good idea.I took the AsyncSpinner from a tutorial. Why is it bad? The error was somewhere else though.
In almost 99% of the cases, callbacks should not start, stop, pause, create or destroy publishers, subscribers, nodehandles, timers, callback queues, spinners, etc, etc. All those things either:
Instances of those things are ..
.. necessary for your ROS node to function, but callbacks are not the place to deal with them, as it will either not work (because callbacks don't (or shouldn't) run long enough to allow proper initialisation (in the case of things like NodeHandles, pubs, subs) or it will cause your control ..
.. flow to be strangely affected (seemingly missing messages, callbacks executed in strange order, etc).
An
AsyncSpinner
is not necessarily bad, but given the above, it should be created in the initialisation phase of your node.Whenever you feel you need something like that inside a callback ..
.. it is typically an indication of a conceptual issue that you're not handling correctly.
Can you explain why you instantiate that spinner in the callback?
Thanks for the explanation. I wasn't really sure what it does. However I am working with moveit in my nodes. I just commented all the spinners outside the main loop. Now my functions can still be used as before but only once. Without the spinner, even such small callbacks ....
... get stuck after execution, waiting for some return statements I guess. The function is only properly finished, if I wrap the whole thing into an additional spinner like I did in the example above. By the way: Without the spinners the calls were faster (but got stuck at the end like I mentioned)