R
Ralf Goertz
Hi,
I have now spent a day to track down a segmentation fault in a program
of mine that used to work. This time it crashed always exactly at the
same place (I was running it with new data). The program uses
unordered_multimap<string, SomeClass> and I repeatedly call a subroutine
with two const SomeClass & elements obtained from iterators of that map.
gdb told me that the segfault occured at the line where I called the
subroutine. Only after switching off optimization (which in this case is
very expensive because the map contains millions of elements and
execution time increased from 20 minutes to over an hour) I got a
different source line where the error occured:
Program terminated with signal 11, Segmentation fault.
#0 0x000000000041f642 in std::_Rb_tree<std::string, std::string, std::_Identity<std::string>, std::less<std::string>,
std::allocator<std::string> >::_M_insert_unique (
this=0x7fff600cf070, linknodes=...) at /usr/include/c++/4.5/bits/stl_tree.h:1181
In that file we have
1171 template<typename _Key, typename _Val, typename _KeyOfValue,
1172 typename _Compare, typename _Alloc>
1173 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1174 _Compare, _Alloc>::iterator, bool>
1175 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1176 _M_insert_unique(const _Val& __v)
1177 {
1178 _Link_type __x = _M_begin();
1179 _Link_type __y = _M_end();
1180 bool __comp = true;
1181 while (__x != 0)
1182 {
1183 __y = __x;
1184 __comp = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x));
1185 __x = __comp ? _S_left(__x) : _S_right(__x);
1186 }
1187 iterator __j = iterator(__y);
1188 if (__comp)
1189 {
1190 if (__j == begin())
1191 return pair<iterator, bool>(_M_insert_(__x, __y, __v), true);
1192 else
1193 --__j;
1194 }
1195 if (_M_impl._M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
1196 return pair<iterator, bool>(_M_insert_(__x, __y, __v), true);
1197 return pair<iterator, bool>(__j, false);
1198 }
I still don't see where there should be a problem but after switching to
boost::unordered_multimap every thing works fine again.
I don't want to exclude the possibility that my program is buggy but
given that
* it had worked previously,
* works fine with boost,
* crashes only with specific input after many executions of that line
* doesn't use any pointers at all
what are the odds that the g++ implementation is the culprit? Maybe there
is something wrong with the hashing?
I have now spent a day to track down a segmentation fault in a program
of mine that used to work. This time it crashed always exactly at the
same place (I was running it with new data). The program uses
unordered_multimap<string, SomeClass> and I repeatedly call a subroutine
with two const SomeClass & elements obtained from iterators of that map.
gdb told me that the segfault occured at the line where I called the
subroutine. Only after switching off optimization (which in this case is
very expensive because the map contains millions of elements and
execution time increased from 20 minutes to over an hour) I got a
different source line where the error occured:
Program terminated with signal 11, Segmentation fault.
#0 0x000000000041f642 in std::_Rb_tree<std::string, std::string, std::_Identity<std::string>, std::less<std::string>,
std::allocator<std::string> >::_M_insert_unique (
this=0x7fff600cf070, linknodes=...) at /usr/include/c++/4.5/bits/stl_tree.h:1181
In that file we have
1171 template<typename _Key, typename _Val, typename _KeyOfValue,
1172 typename _Compare, typename _Alloc>
1173 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1174 _Compare, _Alloc>::iterator, bool>
1175 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1176 _M_insert_unique(const _Val& __v)
1177 {
1178 _Link_type __x = _M_begin();
1179 _Link_type __y = _M_end();
1180 bool __comp = true;
1181 while (__x != 0)
1182 {
1183 __y = __x;
1184 __comp = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x));
1185 __x = __comp ? _S_left(__x) : _S_right(__x);
1186 }
1187 iterator __j = iterator(__y);
1188 if (__comp)
1189 {
1190 if (__j == begin())
1191 return pair<iterator, bool>(_M_insert_(__x, __y, __v), true);
1192 else
1193 --__j;
1194 }
1195 if (_M_impl._M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
1196 return pair<iterator, bool>(_M_insert_(__x, __y, __v), true);
1197 return pair<iterator, bool>(__j, false);
1198 }
I still don't see where there should be a problem but after switching to
boost::unordered_multimap every thing works fine again.
I don't want to exclude the possibility that my program is buggy but
given that
* it had worked previously,
* works fine with boost,
* crashes only with specific input after many executions of that line
* doesn't use any pointers at all
what are the odds that the g++ implementation is the culprit? Maybe there
is something wrong with the hashing?