0
$\begingroup$

Rosanswers logo

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());
      return;
   }

   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/libroscpp.so)
==11677==    by 0x40D8A86: ros::CallbackQueue::callOneCB(ros::CallbackQueue::TLS*) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x40DA563: ros::CallbackQueue::callAvailable(ros::WallDuration) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==  Address 0x1ca8f500 is 921,616 bytes inside a block of size 921,624 free'd
==11677==    at 0x402C06C: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==11677==    by 0x43EE3D1: cv::fastFree(void*) (in /opt/ros/fuerte/lib/libopencv_core.so.2.4.2)
==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/libroscpp.so)
==11677==    by 0x40D8A86: ros::CallbackQueue::callOneCB(ros::CallbackQueue::TLS*) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x40DA563: ros::CallbackQueue::callAvailable(ros::WallDuration) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x411039A: ros::spinOnce() (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x48FF4D2: (below main) (libc-start.c:226)
==11677== 
==11677== Invalid read of size 4
==11677==    at 0x43EE3C7: cv::fastFree(void*) (in /opt/ros/fuerte/lib/libopencv_core.so.2.4.2)
==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/libroscpp.so)
==11677==    by 0x40D8A86: ros::CallbackQueue::callOneCB(ros::CallbackQueue::TLS*) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x40DA563: ros::CallbackQueue::callAvailable(ros::WallDuration) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==  Address 0x1c9ae4fc is 12 bytes inside a block of size 921,624 free'd
==11677==    at 0x402C06C: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==11677==    by 0x43EE3D1: cv::fastFree(void*) (in /opt/ros/fuerte/lib/libopencv_core.so.2.4.2)
==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/libroscpp.so)
==11677==    by 0x40D8A86: ros::CallbackQueue::callOneCB(ros::CallbackQueue::TLS*) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x40DA563: ros::CallbackQueue::callAvailable(ros::WallDuration) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x411039A: ros::spinOnce() (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x48FF4D2: (below main) (libc-start.c:226)
==11677== 
==11677== Invalid free() / delete / delete[] / realloc()
==11677==    at 0x402C06C: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==11677==    by 0x43EE3D1: cv::fastFree(void*) (in /opt/ros/fuerte/lib/libopencv_core.so.2.4.2)
==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/libroscpp.so)
==11677==    by 0x40D8A86: ros::CallbackQueue::callOneCB(ros::CallbackQueue::TLS*) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==  Address 0x1c9ae4f0 is 0 bytes inside a block of size 921,624 free'd
==11677==    at 0x402C06C: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==11677==    by 0x43EE3D1: cv::fastFree(void*) (in /opt/ros/fuerte/lib/libopencv_core.so.2.4.2)
==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/libroscpp.so)
==11677==    by 0x40D8A86: ros::CallbackQueue::callOneCB(ros::CallbackQueue::TLS*) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x40DA563: ros::CallbackQueue::callAvailable(ros::WallDuration) (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x411039A: ros::spinOnce() (in /opt/ros/fuerte/lib/libroscpp.so)
==11677==    by 0x48FF4D2: (below main) (libc-start.c:226)
==11677== 

Originally posted by ocli on ROS Answers with karma: 215 on 2012-10-02

Post score: 1

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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( &cv_ptr->image );


Originally posted by Lorenz with karma: 22731 on 2012-10-04

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by joq with karma: 25443 on 2012-10-02

This answer was NOT ACCEPTED on the original site

Post score: 2


Original comments

Comment by ocli on 2012-10-02:
Already tried that, I get a segmentation fault so I removed it - the cv_ptr->image cannot be released apparently.

Comment by ocli on 2012-10-04:
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?

Comment by ocli on 2012-10-04:
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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.