删除指针,C ++,Debug Assertion失败

时间:2013-05-04 18:59:28

标签: c++ visual-c++

我在运行时遇到错误“Debug Assertion Failed”,代码如下。我用MVC ++ 2010编译代码,在调试模式下,当我点击错误窗口上的重试时,它将我引导到文件dbgdel.cpp的行

_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

当我评论该行

delete result;

在代码中,错误消失......

感谢您帮助我们理解这个问题。我可以删除指针结果,new用于使用clone方法创建它 (对不起,我无法发布完整的代码,而且太大了)

感谢您的帮助

/***CODE****/



########

File: DrawShapeBase.cpp 
...
...

Base* DrawShapeBase::SetOutput(Base* data)
{
   return (data->clone());
}


....
Base* DrawShapeBase::Draw(Base* data){
    Base* result = setOutput(data); 
        ...
    ...
    return result; 

}

/*##############*/
File: Derived.cpp

...
...

Base* Derived::clone()
{
    Base* b = new Derived(*this);
    return b;
}

...
...

/*##############*/
File Base.h
public:

    Base();
    virtual ~Base();
    virtual Base* clone(void) = 0;
....


/*##############*/
main.cpp 


...
...
Base* data = new Derived();
DrawBase* dr = new DrawShapeBase();
Base* result = dr->Draw(data);

delete data;
delete result;
delete dr; 
...

0 个答案:

没有答案