ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

cv_bridge memory issues

asked 2012-10-02 04:05:07 -0600

ocli gravatar image

updated 2012-10-03 22:52:34 -0600

I'm having problems with memory when it comes to using the cv_ptr->image from this tutorial. How would I copy the image to use it as an IplImage* for the rest of the program so that I can release all data at the end?

This is my latest attempt - imageBGR is for altering, imageRaw is the raw image to be sent around to a few functions/callbacks etc.

void ImageCb(const sensor_msgs::ImageConstPtr &msg)
   cv_bridge::CvImagePtr cv_ptr;
   try {
   cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
   catch (cv_bridge::Exception& e){
      ROS_ERROR("cv_bridge exception: %s", e.what());

   IplImage* imageRaw, *imageBGR;
   imageRaw = cvCreateImage( cvSize(640,480) ,IPL_DEPTH_8U,3);
   *imageRaw = cv_ptr->image;
   imageBGR = cvCloneImage( imageRaw );
   //do stuff
   cvReleaseImage( &imageBGR );

I can't use CvMat because I'm using an obsolete package that takes in IplImage*.

I get the following error after running the code above for a bit

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

If I release the imageRaw image, then I get a segmentation fault and this output from valgrind

==11677== Invalid read of size 4
==11677==    at 0x4229CD6: boost::detail::sp_counted_impl_pd<cv_bridge::CvImage*, boost::detail::sp_ms_deleter<cv_bridge::CvImage> >::dispose() (mat.hpp:366)
==11677==    by 0x807781D: boost::detail::shared_count::~shared_count() (shared_count.hpp:217)
==11677==    by 0x80787FF: boost::shared_ptr<cv_bridge::CvImage>::~shared_ptr() (shared_ptr.hpp:168)
==11677==    by 0x8073D99: ImageCb(boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&) (state_sensor.cpp:173)
==11677==    by 0x807DCA8: boost::detail::function::void_function_invoker1<void (*)(boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&), void, boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&>::invoke(boost::detail::function::function_buffer&, boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&) (function_template.hpp:112)
==11677==    by 0x4759603: image_transport::RawSubscriber::internalCallback(boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&, boost::function<void ()(boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&)> const&) (function_template.hpp:1013)
==11677==    by 0x4756802: boost::detail::function::void_function_obj_invoker1<boost::_bi::bind_t<void, boost::_mfi::mf2<void, image_transport::SimpleSubscriberPlugin<sensor_msgs::Image_<std::allocator<void> > >, boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&, boost::function<void ()(boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&)> const&>, boost::_bi::list3<boost::_bi::value<image_transport::SimpleSubscriberPlugin<sensor_msgs::Image_<std::allocator<void> > >*>, boost::arg<1>, boost::_bi::value<boost::function<void ()(boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&)> > > >, void, boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&>::invoke(boost::detail::function::function_buffer&, boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&) (mem_fn_template.hpp:280)
==11677==    by 0x474C965: boost::detail::function::void_function_obj_invoker1<boost::function<void ()(boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&)>, void, boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> >::invoke(boost::detail::function::function_buffer&, boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const>) (function_template.hpp:1013)
==11677==    by 0x475FBFE: ros::SubscriptionCallbackHelperT<boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&, void>::call(ros::SubscriptionCallbackHelperCallParams&) (function_template.hpp:1013)
==11677==    by 0x4128C06: ros::SubscriptionQueue::call() (in /opt/ros/fuerte/lib/
==11677==    by 0x40D8A86: ros::CallbackQueue::callOneCB(ros::CallbackQueue ...
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-10-04 06:43:21 -0600

Lorenz gravatar image

You are first creating an image with cvCreateImage and then overwriting the pointer rawImage by cv_ptr->image. That's probably your memory leak. Why aren't you just cloning the image in cv_ptr? imageBGR = cvCloneImage( &amp;cv_ptr-&gt;image );

edit flag offensive delete link more

answered 2012-10-02 06:17:11 -0600

joq gravatar image

updated 2012-10-05 05:18:14 -0600

Looks like you are not releasing the *ImageRaw memory. Every time your callback returns, it loses that image.

EDIT: my original answer above was wrong. For future reference: @Lorenz is correct: clone the image, don't over-write an existing pointer.

edit flag offensive delete link more


Already tried that, I get a segmentation fault so I removed it - the cv_ptr->image cannot be released apparently.

ocli gravatar image ocli  ( 2012-10-02 19:23:51 -0600 )edit

The segmentation fault actually occurs after the callback scope. It seems like, inside the function, I free 921,624 bytes of image data. And after the function boost, ROS and OpenCV processes release memory 0, 12 and 921,616 bytes into that free'd memory block, respectively. Any ideas?

ocli gravatar image ocli  ( 2012-10-04 00:03:52 -0600 )edit

Hmm, that did seem to be the problem, but I thought I had done that in the past. As a side, do you know why it takes a while to process the point cloud from the openni package? between any other callback and a callback involving sensor_msgs::PointCloud2 there is a significant delay

ocli gravatar image ocli  ( 2012-10-04 22:30:27 -0600 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2012-10-02 04:05:07 -0600

Seen: 929 times

Last updated: Oct 05 '12