Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ STL list functions segfaulting with empty list
    text
    copied!<p>I have a list of pointers as a member of a class. I instantiate that class, and various functions such as size() and empty() fail with a segfault when the list is empty. When I add something to the list, they're fine. I tried to abstract what I'm doing with a test file, and it works perfectly. This is what I THINK I'm doing when my code fails (though am clearly not):</p> <pre><code>#include &lt;list&gt; #include &lt;iostream&gt; class Test { int i; }; int main() { std::list&lt;Test*&gt; tlist; if (tlist.empty()) { std::cout &lt;&lt; "List empty"; } else { std::cout &lt;&lt; "List not empty"; } } </code></pre> <p>I can't really post the entire code listing that's causing the issue, as it's fairly large and over a bunch of files, but will try to paste all the relevant bits straight from code:</p> <p>Class declaration in player.h:</p> <pre><code>class Player : public ScreenObject { private: std::list&lt;Thing*&gt; inventory; </code></pre> <p>Nothing is done to that list in the constructor.</p> <p>Where it's failing:</p> <p>main.cpp:</p> <pre><code>Player pc(iname, w_choice, c_choice, 11, 11, WHITE, '@'); </code></pre> <p>....</p> <pre><code>if (pc.addToInv(t)) { currentLevel.delObject(id); } </code></pre> <p>....</p> <p>player.cpp:</p> <pre><code>int Player::addToInv(Thing&amp; t) { if (inventory.size() &lt;= 52) { inventory.push_back(&amp;t); } else { shiplog("Cannot add to inventory, 52 item limit reached",10); return 0; } } </code></pre> <p>The error I get when running it with gdb occurs on the call to size(), and ends up here:</p> <pre><code>Program received signal SIGSEGV, Segmentation fault. 0x0804eda6 in std::_List_const_iterator&lt;Thing*&gt;::operator++ (this=0xbfff9500) at /usr/include/c++/4.4/bits/stl_list.h:223 223 _M_node = _M_node-&gt;_M_next; </code></pre> <p>Any guesses much appreciated!</p> <hr> <p>Full backtrace is:</p> <pre><code>(gdb) bt 0 0x0804e28a in std::_List_const_iterator&lt;Thing*&gt;::operator++ ( this=0xbfff9500) at /usr/include/c++/4.4/bits/stl_list.h:223 1 0x0804e64e in std::__distance&lt;std::_List_const_iterator&lt;Thing*&gt; &gt; ( __first=..., __last=...) at /usr/include/c++/4.4/bits/stl_iterator_base_funcs.h:79 2 0x0804e4d3 in std::distance&lt;std::_List_const_iterator&lt;Thing*&gt; &gt; ( __first=..., __last=...) at /usr/include/c++/4.4/bits/stl_iterator_base_funcs.h:114 3 0x0804e2e6 in std::list&lt;Thing*, std::allocator&lt;Thing*&gt; &gt;::size ( this=0xbffff244) at /usr/include/c++/4.4/bits/stl_list.h:805 4 0x0804df78 in Player::addToInv (this=0xbffff068, t=...) at player.cpp:551 5 0x0804a873 in main (argc=1, argv=0xbffff494) at main.cpp:182 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload