ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Problem with ROS_ASSERT

asked 2017-09-13 00:36:04 -0600

johnyang gravatar image

updated 2017-09-13 01:07:04 -0600

I am running into the same problem as: https://answers.ros.org/question/2163...

std::string serial_number_str;
if(!config_nh_.getParam("serial_number", serial_number_str))
{
    ROS_ERROR("You must specify a serial number");
    valid_ = false;
}
else
{
    ROS_ASSERT( SerialNumberFromHex(serial_number_str, &serial_number_) );
    ROS_INFO_STREAM("serial number="<<serial_number_str<<" ,hex-> "<<serial_number_);
}

The exact same code above works on one machine running Ubuntu 14.04 but failed on a PC running 14.02 (both x64)

However, I found out if I removed ROS_ASSERT, SerialNumberFromHex() function will work. If I use the original code, serial_number_ will never obtain the correct reading from the serial_number_str.

Anyone can explain why this is happening? I know I can get it to work if I modify the code, but I really want to understand why this is happening.

Also I tried to put ROS_INFO() to print out the values, I noticed ROS_ASSERT will not allow me to print out anything:

bool SerialNumberFromHex(const std::string& str, uint64_t* serial_number) {
    std::stringstream ss;
    ss << std::hex << str;
    ROS_INFO_STREAM("ss="<<ss.str());
    ss.flush();
    ROS_INFO_STREAM("ss="<<ss.str());
    ss >> *serial_number;
    ROS_INFO_STREAM("ss="<<ss.fail());
    return true;
}

This kinda tells me maybe the SerialNumberFromHex() function is never called? So I tried ROS_ASSERT(false) directly, and surprisingly it didn't throw fatal error as expected. So something wrong with ROS_ASSERT command on my machine but without compilation error. Anyone has any clue what's causing the problem (like compiler problem etc)?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-05-20 02:15:25 -0600

tfoote gravatar image

You should check what build flags you're running. If you have turned on optimizations and are building for release asserts will be compiled out for faster operations. If you need the code inside to run in regular operations they should not be done inside an assert.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-13 00:36:04 -0600

Seen: 745 times

Last updated: May 20 '19