i'm using vector container hold instances of object contain 3 ints , 2 std::string
s, created on stack , populated function in class running app through deleaker shows std::string
s object leaked. here's code:
// populator function: void populatorclass::populate(std::vector<myclass>& list) { // m_mainlist contains list of pointers master objects for( std::vector<myclass*>::iterator = m_mainlist.begin(); != m_mainlist.end(); it++ ) { list.push_back(**it); } } // class definition class myclass { private: std::string m_name; std::string m_description; int m_ntype; int m_ncategory; int m_nsubcategory; }; // code causing problem: std::vector<myclass> list; populatorclass.populate(list);
when run through deleaker leaked memory in allocator std::string
classes.
i'm using visual studio 2010 (crt).
is there special need make string
s delete when unwinding stack , deleting vector
?
thanks, j
every time got problem stl implementation doing strange or wrong memory leak, try this :
- reproduce basic example of try achieve. if runs without leak, problem in way fill data. it's probable source of problem (i mean own code).
not tested simple on-the-fly example specific problem :
#include <string> #include <sstream> // class definition struct myclass { // struct convenience std::string m_name; std::string m_description; int m_ntype; int m_ncategory; int m_nsubcategory; }; // prototype of populator function: void populate(std::vector<myclass>& list) { const int max_type_idx = 4; const int max_category_idx = 8; const int max_sub_category_idx = 6; for( int type_idx = 0; type_idx < max_type_idx ; ++type_idx) for( int category_idx = 0; category_idx < max_category_idx ; ++category_idx) for( int sub_category_idx = 0; sub_category_idx < max_sub_category_idx ; ++sub_category_idx) { std::stringstream name_stream; name_stream << "object_" << type_idx << "_" << category_idx << "_" << sub_category_idx ; std::stringstream desc_stream; desc_stream << "this object of type n°" << type_idx << ".\n"; desc_stream << "it of category n°" << category_idx << ",\n"; desc_stream << "and of sub-category n°" << category_idx << "!\n"; myclass object; object.m_name = name_stream.str(); object.m_description = desc_stream.str(); object.m_ntype = type_idx; m_ncategory = m_nsubcategory = list.push_back( object ); } } int main() { // code causing problem: std::vector<myclass> list; populate(list); // memory leak check? return 0; }
- if still got memory leak, first check it's not false-positive leak detection software.
- then if it's not, google memory leak problems stl implementation (most of time on compiler developer website). implementor might provide bug tracking tool search in same problem , potential solution.
- if still can't find source of leak, maybe try build project different compiler (if can) , see if have same effect. again if leak still occurs, problem have lot of chances come code.
Comments
Post a Comment