这是我编写的代码:

class sCircBuffer
{
    public:
        sCircBuffer(void);
        ~sCircBuffer(void);
        double *Data;
        int Size;
        bool Init(int SizeBuffer);
        bool Delete();
}

sCircBuffer :: sCircBuffer(void) //Constructor
{
    Data=NULL; //Initialize input circular buffer
}

sCircBuffer :: ~sCircBuffer(void) //Destructor
{
    delete [] Data; //Initialize input circular buffer
    Data=NULL;
}

bool sCircBuffer :: Init(int SizeBuffer)
{
    delete [] Data; //Initialize input circular buffer
    Data=new double [SizeBuffer]; //Initialize input circular buffer
    Size=SizeBuffer;
    for (int i=0; i<Size; i++)
        Data[i]=0;
    return true;
}

bool sCircBuffer :: Delete()
{
    delete [] Data; //Initialize input circular buffer
    Data=NULL;
    return true;
}


我在另一个类中创建上述类的对象:

class PerChannel
{
    public:
        PerChannel();
        ~PerChannel();

        sCircBuffer m_InputDataRaw;
}

PerChannel :: PerChannel()
{
    m_InputDataRaw.Init(MAX_NUM_TO_FETCH); // MAX_NUM_TO_FETCH = 1000
}

PerChannel :: ~PerChannel()
{
    m_InputDataRaw.Delete();
}


在Coverity和C ++内存验证器中,我在PerChannel的构造函数中遇到资源泄漏错误。

我不确定这里出什么问题了吗?

非常感谢您的帮助。

金坛

最佳答案

initsCircbuffer方法中,尝试delete []指向NULL的指针。

关于c++ - 由于删除运算符导致资源泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17812661/

10-11 16:47