Thursday, 15 May 2014

C++ STL list functions segfaulting with empty list -



C++ STL list functions segfaulting with empty list -

i have list of pointers fellow member of class. instantiate class, , various functions such size() , empty() fail segfault when list empty. when add together list, they're fine. tried abstract i'm doing test file, , works perfectly. think i'm doing when code fails (though not):

#include <list> #include <iostream> class test { int i; }; int main() { std::list<test*> tlist; if (tlist.empty()) { std::cout << "list empty"; } else { std::cout << "list not empty"; } }

i can't post entire code listing that's causing issue, it's big , on bunch of files, seek paste relevant bits straight code:

class declaration in player.h:

class player : public screenobject { private: std::list<thing*> inventory;

nothing done list in constructor.

where it's failing:

main.cpp:

player pc(iname, w_choice, c_choice, 11, 11, white, '@');

....

if (pc.addtoinv(t)) { currentlevel.delobject(id); }

....

player.cpp:

int player::addtoinv(thing& t) { if (inventory.size() <= 52) { inventory.push_back(&t); } else { shiplog("cannot add together inventory, 52 item limit reached",10); homecoming 0; } }

the error when running gdb occurs on phone call size(), , ends here:

program received signal sigsegv, segmentation fault. 0x0804eda6 in std::_list_const_iterator<thing*>::operator++ (this=0xbfff9500) @ /usr/include/c++/4.4/bits/stl_list.h:223 223 _m_node = _m_node->_m_next;

any guesses much appreciated!

full backtrace is:

(gdb) bt 0 0x0804e28a in std::_list_const_iterator<thing*>::operator++ ( this=0xbfff9500) @ /usr/include/c++/4.4/bits/stl_list.h:223 1 0x0804e64e in std::__distance<std::_list_const_iterator<thing*> > ( __first=..., __last=...) @ /usr/include/c++/4.4/bits/stl_iterator_base_funcs.h:79 2 0x0804e4d3 in std::distance<std::_list_const_iterator<thing*> > ( __first=..., __last=...) @ /usr/include/c++/4.4/bits/stl_iterator_base_funcs.h:114 3 0x0804e2e6 in std::list<thing*, std::allocator<thing*> >::size ( this=0xbffff244) @ /usr/include/c++/4.4/bits/stl_list.h:805 4 0x0804df78 in player::addtoinv (this=0xbffff068, t=...) @ player.cpp:551 5 0x0804a873 in main (argc=1, argv=0xbffff494) @ main.cpp:182

int player::addtoinv(thing& t) { if (inventory.size() <= 52) { inventory.push_back(&t); } else { shiplog("cannot add together inventory, 52 item limit reached",10); homecoming 0; }

}

thing passed reference, address passed inventory.push_back(). seek passing 't'.

c++ list stl

No comments:

Post a Comment