c++11 - C++ - Using list<> in struct to create a tree structure -


i've been using std::list<> inside of struct create tree-like structure. compiles (-std=c++11) , runs no problem have been worried because didn't quite right. lately code started give me segmentation fault , stack shows issue occurring destructors in lists. below code snippet of doing:

#include <string> #include <list>  struct node {     std::string key;     std::string value;     std::list<node> children;      node() {}     node(std::string key, std::string value)      : key(key), value(value) {} }; 

here stack trace getting gdb (what below , repeats several times):

> malloc_consolidate() > _int_free() > __gnu_cxx::new_allocator<std::_list_node<node> >::deallocate() > std::__cxx11::_list_base<node, std::allocator<node> >::_m_put_node() > std::__cxx11::_list_base<node, std::allocator<node> >::_m_clear() > std::__cxx11::_list_base<node, std::allocator<node> >::~_list_base() > std::__cxx11::list<node, std::allocator<node> >::~list() > node::~node() > ... 

the spot in malloc.c crashes malloc_consolidate() line 4163 @ nextsize = chunksize(nextchunk);. below portion malloc.c segmentation fault occurs (gcc v5.2):

/* streamlined version of consolidation code in free() */ size = p->size & ~(prev_inuse|non_main_arena); nextchunk = chunk_at_offset(p, size); nextsize = chunksize(nextchunk);   // **segmentation fault on line!!! 

like i've said have been using awhile lately giving me run-time errors in 1 spot in code. thought destructors std::list<> objects called hit point there lists no objects, node::children.size() = 0, , memory cleanup shouldn't issue since there shouldn't infinite recursion in doing so. if i'm missing please let me know.

update:

as stated in comment igor tandetnik did have memory bug in section of code loosely tied editing data inside of struct node. have corrected bug , works correctly. i'm not going go through details of solution since specific code only, others reading , having similar issue @ functions touch or modify struct , see if there memory issues. dynamic memory, pointers being misused, etc. know everyone's scenario specific own code above tips make easier you.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -