简体   繁体   English

访问cv_ptr-> image时,类中的ROS cv_bridge导致segfault

[英]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); 我遇到一个问题,当我在回调函数中获取我的const sensor_msgs::ImageConstPtr &msg并执行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. 其中cv_ptrcv_bridge::CvImagePtr ,并尝试使用cv_ptr->image做任何事情,这是我执行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? 如果我cv_ptr->image尝试clone cv_ptr->image ,则不会出现段错误...有任何提示或技巧吗? Am I misunderstanding how things are working here...? 我是不是误解了这里的运作方式...?

Edit: I am running my camera data through a looping rosbag with --clock enabled. 编辑:我正在通过启用--clock的循环rosbag运行我的相机数据。

try debugging that using a try/catch block for "cv_bridge::toCvCopy" 尝试使用try / catch块对“ 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. 这应该很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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