ImageTransport throws Segmentation Fault

asked 2015-05-24 08:14:56 -0500

jdorfsman gravatar image

I'm trying to receive an image from a ROS channel in a larger system. This is the function:

Mat* RosNetwork::getImage(string channel) {
_mtx.lock();
ros::NodeHandle nh;
cout << "get image 0" << endl;
image_transport::ImageTransport it(nh);
cout << "get image 1" << endl;
image_transport::Subscriber sub = it.subscribe(channel, 1,
        imageCallback);
cout << "get image 2" << endl;
while (!imageFound) {
    ros::spinOnce();
}
cout << "Image found" << endl;
_mtx.unlock();
return &image;
}

This is the callback function:

Mat image;
bool imageFound = false;
void imageCallback(const sensor_msgs::ImageConstPtr& msg) {
    try {
        image = cv_bridge::toCvShare(msg, "bgr8")->image;
        if (!image.empty()) {
            imageFound = true;
        }
    } catch (cv_bridge::Exception& e) {
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.",
                msg->encoding.c_str());
    }
}

When I run the code I get an Segmentation Fault in the image_transport::ImageTransport it(nh); line. This is run by a THREAD (if it changes something..). Sometimes it runs a few times and then gets a segmentation fault, and sometimes at the first run it gets. Any thoughts?

edit retag flag offensive close merge delete