简体   繁体   中英

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

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.

try debugging that using a try/catch block for "cv_bridge::toCvCopy"

cv_bridge::CvImagePtr cv_ptr;
try
{
  cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
}
catch (cv_bridge::Exception& e)
{
  ROS_ERROR("cv_bridge exception: %s", e.what());
  return;
}

and add the exception description when it occurs.

A quick fix would be changing the line :

cv::Mat image = cv_ptr->image.clone();

to :

cv::Mat image = cv_ptr->image;

This should work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM