首页 > 解决方案 > Coverity 指向潜在的内存泄漏

问题描述

Coverity 在以下代码中指出了潜在的内存泄漏。

m_threads.create_thread(boost::bind(&boost::asio::io_service::run, &m_ioService));

覆盖率误差如下:

CID 223450 (#1 of 1): Resource leak (RESOURCE_LEAK)
8. leaked_storage: Ignoring storage allocated by this->m_threads.create_thread(boost::bind(&run, &this->m_ioService)) leaks it

create_thread 的代码如下:

template<typename F>
thread* create_thread(F threadfunc)
{
    boost::lock_guard<shared_mutex> guard(m);
    std::auto_ptr<thread> new_thread(new thread(threadfunc));
    threads.push_back(new_thread.get());
    return new_thread.release();
}

new_thread 的类型是:

std::auto_ptr<thread> new_thread(new thread(threadfunc));

线程的类型是:

std::list<thread*> threads;

类 thread_group 的析构函数(具有方法 create_thread 和成员线程)删除所有线程:

  for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
     it!=end;
     ++it)
 {
     delete *it;
 }

基于此,您认为是否存在内存泄漏或此错误。

标签: c++coverity

解决方案


推荐阅读