每帧在shared_ptr中动态分配图像
我在这里第一次尝试使用shared_ptr,但是在执行此操作时遇到了一些问题。
我想每帧获取 IplImage 并分配给shared_ptr 类成员,释放最后一个图像。它是这样的:
class Detector {
public:
void Detector::updateImage {
main_image_.reset(cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3));
}
private:
boost::shared_ptr<IplImage> main_image_;
}
我在循环中调用 updateImage 。 cvCreateImage 为该图像大小动态分配一些内存。
第一次循环运行时,一切正常。现在,我第二次收到 _BLOCK_TYPE_IS_VALID 断言错误。当shared_ptr尝试删除指针时会发生这种情况。
因此,假设我做错了什么,我尝试了许多其他选项,例如:
if (!main_image_)
main_image_ = boost::shared_ptr<IplImage> (cvCreateImage...
else
main_image_.reset(cvCreateImage...)
也不起作用。首先尝试重置shared_ptr,也不起作用。尝试设置一个新的临时shared_ptr并分配给我的main_image_ptr。没有成功。
我这里哪里出错了?使用常规指针并手动释放图像非常有效。
提前致谢,
西奥
I'm trying to use shared_ptr for the first time here, but I'm having some trouble doing this.
I want to get am IplImage every frame and allocate to a shared_ptr class member, releasing the last image. It's something like this:
class Detector {
public:
void Detector::updateImage {
main_image_.reset(cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3));
}
private:
boost::shared_ptr<IplImage> main_image_;
}
I call updateImage in a loop. cvCreateImage dynamically allocates some memory for that image size.
The first time the loop runs, everything works ok. Now, the second time I get a _BLOCK_TYPE_IS_VALID assertion error. This happens when shared_ptr is trying to delete the pointer.
So, assuming I was doing something wrong, I tried many other options like:
if (!main_image_)
main_image_ = boost::shared_ptr<IplImage> (cvCreateImage...
else
main_image_.reset(cvCreateImage...)
Didn't work also. Tried resetting the shared_ptr first, didn't work either. Tried setting a new temporary shared_ptr and assigning to my main_image_ ptr. No success.
Where am I going wrong here? Using regular pointers and releasing the image manually worked like a charm.
Thanks in advance,
Theo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您在调试版本中看到此错误?
cvCreateImage()
使用哪种分配内存的方法?new
还是malloc()
?boost::shared_ptr
使用delete
来销毁内存,因此您的系统可能会检测到数据没有以“正确的方式”分配,即通过使用新的。如果是这种情况,那么您必须将
shared_ptr
与自定义删除器一起使用(请参阅 boost 文档以获取更多信息),以便正确释放内存。I assume that you're seeing this error in a debug build?
Which method of allocating memory does
cvCreateImage()
use?new
ormalloc()
?boost::shared_ptr
usesdelete
to destroy the memory so there might be a chance that your system detects that the data wasn't allocated the "right way", ie by using new.If that's the case then you'd have to use a
shared_ptr
with a custom deleter (look at the boost docs for more info) so the memory gets released correctly.