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