模板类导致析构函数被调用两次

时间:2016-10-15 15:29:21

标签: c++ templates

我有一个pipe-filtern模板类。类模板如下所示:

template<class T, class O, class F>
class hdm::filter::Filter

T是输入类型类,O是输出类型。 F是实现过滤器的具体过滤器类 - 但现在这应该是无关紧要的。

过滤器模板类的运行循环是:

void run() noexcept
    {
        pthread_setname_np(getThreadName());
        while(_running) {
            T obj;
            _skip = false;
            while(!_inputqueue->wait_dequeue_timed(obj,chrono::milliseconds(IDLE_LOOP)) && _running);
            if(!_running) break;
            O objout = reinterpret_cast<F*>(this)->process(obj);
            if(_outputqueue != nullptr && !_skip) {
                push_results(objout);
            }
        }
    }

在其中一个具体的过滤器类中,过滤器模板类处理的对象“O”的构造如下:

std::vector<CellLoadInfo*>* referencedCells = new std::vector<CellLoadInfo*>;

下一个过滤器只获取一个CellLoadInfo对象。

现在即使这是一个指针对象,当对象超出上面过滤器的运行循环范围时,似乎会调用CellLoadInfo的析构函数两次。

为什么要两次调用它,以及如何避免它?

0 个答案:

没有答案
相关问题