c++ - 每帧在 shared_ptr 中动态分配图像

标签 c++ boost shared-ptr

我在这里第一次尝试使用 shared_ptr,但我在执行此操作时遇到了一些麻烦。

我想在每一帧获取 am 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。没有成功。

我哪里错了?使用常规指针并手动释放图像非常有效。

提前致谢

西奥

最佳答案

我假设您是在调试版本中看到此错误?

cvCreateImage() 使用哪种分配内存的方法? new 还是 malloc()boost::shared_ptr 使用 delete 来销毁内存,因此您的系统可能会检测到数据没有以“正确的方式”分配,即通过使用新的。

如果是这种情况,那么您必须将 shared_ptr 与自定义删除器一起使用(查看 boost 文档了解更多信息),以便正确释放内存。

关于c++ - 每帧在 shared_ptr 中动态分配图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5200780/

相关文章:

c++ - 如何轻松地用CGAL在球体上构造Voronoi图?

c++ - 如何使用 boost::spirit::qi 和 std::vector<token_type> 而不是 std::string

C++ shared_ptr 释放多个引用

c++11 - 无法在 std::map 中使用 std::shared_ptr 作为值类型?

c++ - C++:错误C2678:二进制 '=':未找到采用 'const SPoint'类型的左操作数的运算符

c++ - 征求有关数据结构使用的建议

c++ - 指针的 boost 序列化保存指针的十六进制值而不是对象的内容

c++ - boost asio 和 coroutine2 示例

c++ - 具有多个函数的 Py_InitModule - 从 int 到 PyCFunction 的无效转换

c++ - CRTP 模式和 enable_shared_from_this