Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seg fault on std::vector.insert

Tags:

c++

stl

I am having a weird error when calling the insert function on a vector of custom types (which are basically graph nodes). Following is the code that is generating the seg fault:

69     for (int i = 0; i < size; i++) {
70         PDTNode * node = arguments[i]->getNode();
71         //addChild(node);
72         children.insert(it, node); // children is vector <PDTNode*> 
73         it++;
74         node->addParent(this);
75     }

I double check all inputs and everything is OK and is not NULL. Below is the gdb stack trace that I got for the seg fault:

#0  0x00007ffff4793202 in ?? () from /lib/libc.so.6
#1  0x00007ffff479163a in memmove () from /lib/libc.so.6
#2  0x00000000004289f1 in std::__copy_move<false, true, std::random_access_iterator_tag>::__copy_m<PDTNode*> (__first=0x100bd60, __last=0x10053c8, 
__result=0x1001840) at /usr/include/c++/4.4/bits/stl_algobase.h:378
#3  0x00000000004289ab in std::__copy_move_a<false, PDTNode**, PDTNode**> (__first=0x100bd60, __last=0x10053c8, __result=0x1001840)
    at /usr/include/c++/4.4/bits/stl_algobase.h:397
#4  0x0000000000428973 in std::__copy_move_a2<false, PDTNode**, PDTNode**>     (__first=0x100bd60, __last=0x10053c8, __result=0x1001840)
    at /usr/include/c++/4.4/bits/stl_algobase.h:436
#5  0x000000000042891d in std::copy<PDTNode**, PDTNode**> (__first=0x100bd60, __last=0x10053c8, __result=0x1001840)
at /usr/include/c++/4.4/bits/stl_algobase.h:468
#6  0x00000000004288dc in std::__uninitialized_copy<true>::uninitialized_copy<PDTNode**, PDTNode**> (__first=0x100bd60, __last=0x10053c8, __result=0x1001840)
    at /usr/include/c++/4.4/bits/stl_uninitialized.h:93
#7  0x0000000000428847 in std::uninitialized_copy<PDTNode**, PDTNode**> (__first=0x100bd60, __last=0x10053c8, __result=0x1001840)
    at /usr/include/c++/4.4/bits/stl_uninitialized.h:117
#8  0x00000000004287b8 in std::__uninitialized_copy_a<PDTNode**, PDTNode**, PDTNode*> (__first=0x100bd60, __last=0x10053c8, __result=0x1001840)
    at /usr/include/c++/4.4/bits/stl_uninitialized.h:257
#9  0x0000000000428684 in std::__uninitialized_move_a<PDTNode**, PDTNode**, std::allocator<PDTNode*> > (__first=0x100bd60, __last=0x10053c8, 
    __result=0x1001840, __alloc=...) at /usr/include/c++/4.4/bits/stl_uninitialized.h:267
#10 0x000000000042812e in std::vector<PDTNode*, std::allocator<PDTNode*> >::_M_insert_aux (this=0x1002810, __position=..., __x=@0x7fffffffd538)
    at /usr/include/c++/4.4/bits/vector.tcc:338
#11 0x000000000043c46d in std::vector<PDTNode*, std::allocator<PDTNode*> >::insert (this=0x1002810, __position=..., __x=@0x7fffffffd538)
    at /usr/include/c++/4.4/bits/vector.tcc:126
#12 0x00000000004d3ea9 in PDTFunctionDec::setArguments (this=0x10027e0, arguments=...) at PDTFunction.cpp:72

Any help is highly appreciated.

Thank you

like image 982
AntiClimacus Avatar asked Apr 14 '26 23:04

AntiClimacus


1 Answers

I guess it is an iterator, but by inserting an element you trigger a relocation of the elements in the container and therefore invalidate the iterator. That should be the cause of your segmentation fault.

like image 105
Tim Avatar answered Apr 16 '26 14:04

Tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!