Reputation: 9360
This would be a lot helpful if one can get more information about it from the debugger. For example, the following program
#include <vector>
#include <iostream>
int main()
{
std::vector<int> x = {1, 2, 3};
for (std::vector<int>::iterator it = x.begin(); it != x.end(); ++ it)
{
x.erase(it);
std::cout << *it << std::endl;
}
}
It would be great if gdb
or valgrind
or other debugging method can show an invalid iterator message when *it
is first used, without manually inspecting the issue.
Upvotes: 1
Views: 503
Reputation: 213799
It would be great if gdb or valgrind or other debugging method can show an invalid iterator message when
*it
is first used
You can use libstdc++
debug mode (enabled by -D_GLIBCXX_DEBUG
) to help with bugs like this. Example:
g++ -std=c++11 -g -D_GLIBCXX_DEBUG t.cc && gdb -q ./a.out
(gdb) r
Starting program: /tmp/a.out
/usr/include/c++/4.8/debug/safe_iterator.h:264:error: attempt to
dereference a singular iterator.
Objects involved in the operation:
iterator "this" @ 0x0x7fffffffe1d0 {
type = N11__gnu_debug14_Safe_iteratorIN9__gnu_cxx17__normal_iteratorIPiNSt9__cxx19986vectorIiSaIiEEEEENSt7__debug6vectorIiS6_EEEE (mutable iterator);
state = singular;
references sequence with type `NSt7__debug6vectorIiSaIiEEE' @ 0x0x7fffffffe1d0
}
Program received signal SIGABRT, Aborted.
0x00007ffff7531cc9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 0x00007ffff7531cc9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1 0x00007ffff75350d8 in __GI_abort () at abort.c:89
#2 0x00007ffff7b85fe5 in __gnu_debug::_Error_formatter::_M_error (this=0x7fffffffdf90) at ../../../../../src/libstdc++-v3/src/c++11/debug.cc:781
#3 0x0000000000402144 in __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int*, std::__cxx1998::vector<int, std::allocator<int> > >, std::__debug::vector<int, std::allocator<int> > >::operator* (this=0x7fffffffe1d0) at /usr/include/c++/4.8/debug/safe_iterator.h:262
#4 0x000000000040145c in main () at t.cc:11
(gdb) fr 4
#4 0x000000000040145c in main () at t.cc:11
11 std::cout << *it << std::endl;
Upvotes: 1