0

我正在尝试在我的 OFX​​ 视频插件中的 std::deque 中对视频缓冲区进行编程。我想访问以前处理的图像以处理当前图像。我的想法是将处理后的图像推送到双端队列的前面,如果缓冲区超过最大大小,则从后面弹出它们。当我尝试在从缓冲区中删除图像之前使用 delete 释放图像的内存时,插件会崩溃。我发现我可以在缓冲区中添加一个或多个图像,然后立即删除它们,没有问题。但是,如果我尝试删除在较早周期中添加的图像,它会崩溃。插件由主类OFXPlugin和处理器类myplugin组成。OFXPlugin 的实例会随着时间的推移而停留,

我不确定我使用双端队列的方式是否有问题,是否不允许我释放由 myplugin 的另一个实例分配的内存,或者我是否在做与 OFX 相关的非法行为API。

下面的代码显示了与问题相关的插件的摘录。它基于 OFX 支持示例。delete videoBuffer_.back().img;它在函数中崩溃OFXPlugin::addToVBuff(OFX::Image *img, double t)。我无法捕获异常,显然它是在 OFX API 中处理(忽略)的。

非常感谢你的帮助!

我的插件.h

#include "ofxsImageEffect.h"
#include "ofxsMultiThread.h"
#include "../Support/Plugins/include/ofxsProcessing.H"

#include <deque>

// Video Buffer Element
typedef struct vBuffEl
{
    OFX::Image* img;
    double  time;
} vBuffEl;

inline
bool operator==(const vBuffEl &a, const double b)
{
    return a.time == b;
}


class myplugin : public OFX::ImageProcessor {
protected :
    OFX::Image *_srcImg;
    double   _time;
    OFXPlugin *_opInstance;

public :
    // ctor
    myplugin(OFX::ImageEffect &instance)
    : OFX::ImageProcessor(instance)
    , _srcImg(0)
    , _time(0)
    {}

    void multiThreadProcessImages(OfxRectI procWindow);

    void setOFXPlugin(OFXPlugin* opInstance) {_opInstance = opInstance;}

    OFXPlugin* getOFXPlugin() {return _opInstance;}

    void setTime(double argsTime) {_time = argsTime;}

    double getTime() {return _time;}

    void setSrcImg(OFX::Image *v) {_srcImg = v;}

    OFX::Image* getSrcImg() {return _srcImg;}

};


class OFXPlugin : public OFX::ImageEffect {
protected :
    OFX::Clip *dstClip_;
    OFX::Clip *srcClip_;

    double time_;
    std::deque<vBuffEl> videoBuffer_;

public :

  /** @brief ctor */
  OFXPlugin(OfxImageEffectHandle handle);

  /** @brief dtor */
  ~OFXPlugin();

  /* Override the render */
  virtual void render(const OFX::RenderArguments &args);

  /* get the source Clip */
  OFX::Clip* getSrcClip();

  /* get the current time */
  double getTime();

  /* set up and run a processor */
  void setupAndProcess(myplugin &, const OFX::RenderArguments &args);

  /* add to video buffer */
  void addToVBuff(OFX::Image *img, double t);

  /* fetch a dst image from buffer */
  void fetchDstImageBuff(double t, OFX::Image* &img, bool &buff);
};

我的插件.cpp

#include "myplugin.h"
#include <algorithm>


void myplugin::multiThreadProcessImages(OfxRectI procWindow)
{
    // Do some filtering of the source image and store result in destination image
    myfiltering(_dstImg, _srcImg, procWindow);

    // add to buffer
    _opInstance->addToVBuff(_dstImg, _time);

}


/* set up and run a processor */
void
OFXPlugin::setupAndProcess(myplugin &processor, const OFX::RenderArguments &args)
{
  // get a dst image
  std::auto_ptr<OFX::Image> dst(dstClip_->fetchImage(args.time));
  OFX::BitDepthEnum dstBitDepth       = dst->getPixelDepth();
  OFX::PixelComponentEnum dstComponents  = dst->getPixelComponents();

  // fetch main input image
  std::auto_ptr<OFX::Image> src(srcClip_->fetchImage(args.time));

  // set the images
  processor.setDstImg(dst.get());
  processor.setSrcImg(src.get());

  // set the render window
  processor.setRenderWindow(args.renderWindow);

  // set time
  processor.setTime(args.time);
  time_ = args.time;

  // set OFXPlugin instance
  processor.setOFXPlugin(this);

  // Call the base class process member, this will call the derived templated process code
  processor.process();
}


OFX::Clip* OFXPlugin::getSrcClip()
{
    return srcClip_;
}

/* get the current time */
double
OFXPlugin::getTime()
{
    return time_;
}

// the overridden render function
void
OFXPlugin::render(const OFX::RenderArguments &args)
{
    try {
          myplugin fred(*this);
          setupAndProcess(fred, args);
    } catch (...) {
      outputMessage("ERROR: An unknown error happened!");
    }
}

/* add to video buffer */
void
OFXPlugin::addToVBuff(OFX::Image *img, double t)
{
    try {
        // if frame already exists in buffer, remove
        std::deque<vBuffEl>::iterator it;
        it = find(videoBuffer_.begin(), videoBuffer_.end(), t);
        if(it != videoBuffer_.end())
        {
            delete it->img;
            videoBuffer_.erase(it);
        }

        // add new frame to the front
        vBuffEl e;
        e.time = t;
        e.img = new OFX::Image(img->getPropertySet().propSetHandle());
        memcpy(e.img, img, sizeof(img));
        videoBuffer_.push_front(e);

        // remove elements at the end, if the buffer exceeds the max size
        int LASTIMG_ARRAY_SIZE = 10;
        while(videoBuffer_.size() > LASTIMG_ARRAY_SIZE)
        {
            delete videoBuffer_.back().img;
            videoBuffer_.erase(--(videoBuffer_.end()));
        }
    } catch (...) {
      outputMessage("ERROR: An unknown error happened!");
    }
}

/* fetch a dst image from buffer */
void
OFXPlugin::fetchDstImageBuff(double t, OFX::Image* &img, bool &buff)
{
    try {
        std::deque<vBuffEl>::iterator it;
        it = find(videoBuffer_.begin(), videoBuffer_.end(), t);
        if(it != videoBuffer_.end())
        {
            img = it->img;      // return buffered dst image
            buff = true;
        }
        else
        {
            img = getSrcClip()->fetchImage(t);  // fetch and return src image
            buff = false;
        }
    } catch (...) {
      outputMessage("ERROR: An unknown error happened!");
    }
}
4

1 回答 1

0

该声明

memcpy(e.img, img, sizeof(img));

不符合您的预期。

指针的sizeof操作返回指针的大小,而不是它指向的内容。这意味着在这种情况下,您只复制 4 或 8 个字节(取决于您是在 32 位还是 64 位平台上)。

然而,在那次通话中还隐藏着另一个更糟糕的问题。memcpy如果OFX::Image包含数据成员指针,则复制数据将复制指针而不是数据。是浅拷贝,不是深拷贝。这就是 C++ 具有复制构造函数和复制赋值运算符的原因。

你应该做的是一个简单的作业,并希望OFX::Image遵循三规则

*e.img = *img;
于 2013-01-25T09:20:31.917 回答