Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cv_bridge in a class leads to segfault when cv_ptr->image is accessed

Hi ROS friends,

I'm having an issue where when I get my const sensor_msgs::ImageConstPtr &msg in my callback function, and do a cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8); where cv_ptr is a cv_bridge::CvImagePtr, and try to do anything with the cv_ptr->image, the line where I do toCvCopy segfaults. When I leave the pointer alone and maybe access its contents or something elsewhere in my class, it's seemingly fine... I'm extremely confused.

Here's some of my real code:

void CNNLocalizer::imageCB(const sensor_msgs::ImageConstPtr &msg)
{
  cv_bridge::CvImagePtr cv_ptr;

  try
  {
    cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8);

    ROS_INFO("Attempt!");
    cv::Mat image = cv_ptr->image.clone();
    ROS_INFO("Success!");

    image.convertTo(g_most_recent_image_, CV_32FC3);

    g_img_height_ = msg->height;
    g_img_width_ = msg->width;

    g_got_image_ = true;
  }
  catch (cv_bridge::Exception &e)
  {
    ROS_ERROR("Failed converting the received message: %s", e.what());
    return;
  }
}

If I get rid of the business where I try to clone the cv_ptr->image, there is no segfault... Any tips or tricks? Am I misunderstanding how things are working here...?

cv_bridge in a class leads to segfault when cv_ptr->image is accessed

Hi ROS friends,

I'm having an issue where when I get my const sensor_msgs::ImageConstPtr &msg in my callback function, and do a cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8); where cv_ptr is a cv_bridge::CvImagePtr, and try to do anything with the cv_ptr->image, the line where I do toCvCopy segfaults. When I leave the pointer alone and maybe access its contents or something elsewhere in my class, it's seemingly fine... I'm extremely confused.

Here's some of my real code:

void CNNLocalizer::imageCB(const sensor_msgs::ImageConstPtr &msg)
{
  cv_bridge::CvImagePtr cv_ptr;

  try
  {
    cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8);

    ROS_INFO("Attempt!");
    cv::Mat image = cv_ptr->image.clone();
    ROS_INFO("Success!");

    image.convertTo(g_most_recent_image_, CV_32FC3);

    g_img_height_ = msg->height;
    g_img_width_ = msg->width;

    g_got_image_ = true;
  }
  catch (cv_bridge::Exception &e)
  {
    ROS_ERROR("Failed converting the received message: %s", e.what());
    return;
  }
}

If I get rid of the business where I try to clone the cv_ptr->image, there is no segfault... Any tips or tricks? Am I misunderstanding how things are working here...?

Edit: I am running my camera data through a looping rosbag with --clock enabled.