c++ - Deleting resources managed by a vector -
i want vector hold pointers objects own.
here vector:
private: std::vector<fppvirtual*> m_fapps;
i have created elements this:
m_fapps.push_back(new fpp1(renderingengine)); //fpp* subclasses of fppvirtual m_fapps.push_back(new fpp2(renderingengine)); m_fapps.push_back(new fpp3(renderingengine));
as m_fapps
vector instance variable in class, want make sure class's destructor cleans m_fapps
:
(int i=0, size=m_fapps.size();i<size;++i){ delete m_fapps[i]; }
is acceptable memory management technique? assume loop needed since when vector goes out of scope when owning class destructed, pointers these new
objects removed, right?
as no 1 gave straight forward answer yet - yes, acceptable , way free memory, having declaration of vector
.
this can , should avoided, using smart pointers, @olicharlesworth suggested or using other container, ponited @björnpollex.
Comments
Post a Comment