0

我有一个 shared_ptr 向量,但在从函数调用返回后它被销毁,但只有在从填充函数返回并且元素是第一个和第二个 [0],[1] 之后。

class SettingsFactory
{
public:
    explicit SettingsFactory(std::vector<std::shared_ptr< AppSettingEntity > >); //CTOR
    ~SettingsFactory();

    MDSettings& CreateMarketDataSettings();
    ExecutionSettings& CreateExecutionSettings();
    StrategySettings& CreateStrategySettings();
private:
    std::vector<std::shared_ptr< AppSettingEntity >>& m_vec;
    void Initialize();


};

MDSettings& SettingsFactory::CreateMarketDataSettings()
{
    bool result = false;
    MDSettings* md = new MDSettings;
    int i=0;
    for(auto &&e :m_vec)
    {
        i++;
        auto key = e.get()->GetSettingsKey();
        if(!key.compare("LOGGER_ENABLED"))
        {
             md->populate(e);
            result = true;
        }
       *** the distraction of the shared_ptr only happenn in the loop after the function call to populate****

    }
    if (!result)
    {
        std::string error("didnt load MarketDataSettings ");
        throw (error);
    }
    return *md;
}

void MDSettings::populate(std::shared_ptr<AppSettingEntity>& us)
{
    auto value = us.get()->GetSettingsValue();
    auto key = us.get()->GetSettingsKey();
    auto num = us.use_count();
    m_data[key] = value;//us.get()->GetSettingsValue();
   // m_data[us.get()->GetSettingsKey()] =us.get()->GetSettingsValue();
}

我不明白为什么,当我在填充函数中时,我正在检查引用计数,它是 2,在调用之前它是 1,我还没有离开函数 scoop

4

0 回答 0