标量删除析构函数崩溃

时间:2014-01-17 15:09:55

标签: c++ windows c++11

我正在编写一个与HTTP相关的小C ++程序,但它总是崩溃,我将WinDbg附加到它,堆栈如下(我省略了RetAddr和其他不相关的列):

MSVCR120D!operator delete+0xa0 [f:\dd\vctools\crt\crtw32\misc\dbgdel.cpp @ 52]
xproxy!HttpRequestInitial::`scalar deleting destructor'+0x53
xproxy!<lambda_e1d25e8e9bf0ff4ef442e9ed8e6e27a0>::operator()+0x7c [d:\xproxy\http_container.h @ 61]
xproxy!std::_For_each<HttpObject * __ptr64 * __ptr64,<lambda_e1d25e8e9bf0ff4ef442e9ed8e6e27a0> >+0x56 [d:\sdts\vs2013\vc\include\algorithm @ 24]
xproxy!std::for_each<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<HttpObject * __ptr64> > >,<lambda_e1d25e8e9bf0ff4ef442e9ed8e6e27a0> >+0x15e [d:\sdts\vs2013\vc\include\algorithm @ 35]
xproxy!HttpContainer::reset+0x8e [d:\xproxy\http_container.h @ 62]

代码将调用HttpContainer::reset(),此函数将删除它存储的所有对象,因此将调用这些对象的析构函数,并且删除操作会导致崩溃,但我已经仔细检查了我的代码,我不能找到崩溃的原因,因为HttpRequestInitial的析构函数什么都不做,还有它的父类。

相关代码如下:

class HttpContainer {
public:
    virtual ~HttpContainer() {
        for(auto it = objects_.begin(); it != objects_.end(); ++it)
            if(*it) delete *it;
    }

    virtual void reset() {
        std::for_each(objects_.begin(), objects_.end(),
                      [](HttpObject* object) { if(object) delete object; });
        objects_.clear();
    }

    // ...

private:
    std::vector<HttpObject*> objects_;
};

// HttpRequestInitial inherits from HttpInitial
// HttpInitial inherits from HttpObject
class HttpRequestInitial : public HttpInitial {
public:
    HttpRequestInitial() : HttpInitial(kHttpRequestInitial) {}

    virtual ~HttpRequestInitial() {}

    // ...

private:
    std::string method_;
    std::string uri_;
};

我只能在SO上找到类似于我的this question,但是它的答案说这是一个编译器错误,但我的编译器是VS2013,我没有使用数组而是向量。有什么想法吗?

修改

感谢您的快速更新。是的,我承认我应该使用智能指针,但现在我很好奇“为什么”。通过使用我省略的HttpContainer方法动态分配对象并将其插入到AppendObject(HttpObject *)的实例中,它发生在exe文件(名为Session的类)中,而不是来自一个DLL,代码如下:

void Session::OnClientDataReceived(const boost::system::error_code& e) {
    HttpObject *object = new HttpRequestInitial();
    //... set its property here
    request_->AppendObject(object);
}

0 个答案:

没有答案