Ask Your Question
0

Why are my rostests giving me a std::bad_alloc error?

asked 2017-04-13 15:07:50 -0500

cpagravel gravatar image

I'm porting my ROS code from Indigo on Ubuntu 14.04 to using the newer ROS Kinetic on Ubuntu 16.04. What I've noticed is that, in one particular test, I will occasionally get a std::bad_alloc exception thrown:

C++ exception with description "std::bad_alloc" thrown in the test fixture's constructor

Behaviour

  • Error only occurs when there are more than one TEST_F() functions.

  • Console output stops displaying after first TEST_F() is run.

Code

class TransitionClientTest : public ::testing::Test
{
public:
  void SetUp()
  {
    query_client_.Initialize(node_handle_);
  }

private:
  ros::NodeHandle node_handle_
  QueryClientType query_client_;
};

TEST_F(TransitionClientTest, TestQuery)
{
  ROS_INFO("TestQuery running");
  EXPECT_EQ(query_client_.Query(), 1);
}

TEST_F(TransitionClientTest, TestPort)
{
  ROS_INFO("TestPort running");  // Won't get displayed
  EXPECT_EQ(query_client_.Port(), 99);
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "TransitionClientTest");
  testing::InitGoogleTest(&argc, argv);
  int result = RUN_ALL_TESTS();
  return result;
}

The TestPort test will often cause a std::bad_alloc error. If it doesn't, then it will not display the ROS_INFO statement.

The test is defined in my CMakeLists in a add_rostest_gtest() call.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-04-13 15:10:15 -0500

cpagravel gravatar image

The reason behind this bug is related to a bug reported in roscomm: https://github.com/ros/ros_comm/issue...

To fix this, I changed the int main function to:

int main(int argc, char** argv)
{
  ros::init(argc, argv, "TransitionClientTest");
  testing::InitGoogleTest(&argc, argv);
  ros::NodeHandle nh("~");
  int result = RUN_ALL_TESTS();
  return result;
}

When there was a NodeHandle that persisted, it prevented the std::bad_alloc exception and gave me console output in the proceeding cases.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-04-13 15:07:50 -0500

Seen: 859 times

Last updated: Apr 13 '17