I have a vector of shared_ptr but after return from a function call it is Destroyed, but only after returning from the populate function and the elements are the first and the second one [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();
}
I don't understand why, when I'm in the populate function im checking the reference count and it's 2 and before the call it's 1 and I didn't leave the function scoop yet