在boost中传递构造函数参数时出错:shared_ptr<>

时间:2013-11-23 02:07:40

标签: c++ memory-management boost constructor shared-ptr

我调用以下代码(调试错误与第二行有关):

boost::shared_ptr<EX> e(new LE());
F* f = new F("", e, 2);

我在access violation reading location库中收到调试错误(boost::shared_ptr<>):

    void add_ref_copy()
    {
//                                         ERROR HAPPENS HERE!!!!!!!
        BOOST_INTERLOCKED_INCREMENT( &use_count_ );
    }

这是上述电话中涉及的代码(如果您需要查看更多代码,请与我们联系)。

问题显示当我单步进入F的构造函数时,我会立即“跳过”构造函数签名行。

编辑这就是调用堆栈的样子:

boost::detail::sp_counted_base::add_ref_copy() Line 74  C++
boost::detail::shared_count::shared_count(const boost::detail::shared_count & r) Line 383   C++
boost::shared_ptr<OB>::shared_ptr(const boost::shared_ptr<OB> & r) Line 397 C++
std::std::(boost::shared_ptr<EX> * e) Line 4    C++
boost::boost::(std::unordered_map<std::string, boost::shared_ptr<I>, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<const std::string, boost::shared_ptr<I> > > > *) Line 261 C++

F.cpp

//When the debugger is on this line, stepping over once more causes the error
F::F(boost::shared_ptr<EX> ex) : DV(ob)
{
    //Debugger never reaches this line
    ob = EX->getX();
}

F.h

#include "D.h"
#include <boost\shared_ptr.hpp>


class F: public D{
public:

    F(boost::shared_ptr<EX> e);
    virtual ~F();

    F(const F& f);
    F& operator=(const F& f);

private:
};

DV.cpp

DV::DV(boost::shared_ptr<OB> ob) : I(ob)
{
}

DV::~DV()
{
}

实施例H

#include "OB.h"
#include <boost\shared_ptr.hpp>
#include <unordered_map>

class EX{

public:
    EX();
    EX(const EX& src);
    virtual boost::shared_ptr<OB> getX() = 0;

private:

};

EX.cpp

EX::EX(){
}

EX::EX(const EX& src){

}

LE.h

#include "EX.h"
#include "OBO.h"
#include <boost\shared_ptr.hpp>
#include <unordered_map>

class LE : public EX{

public:

    LE();
    LE(const LE& src);
    virtual boost::shared_ptr<OB> getX();

private:

};

LE.cpp

LE::LE(){
}

LE::LE(const LE& src){

}

boost::shared_ptr<OB> LE::getX(){
    return boost::shared_ptr<OB>(new OBO);
}

0 个答案:

没有答案