mardi 25 avril 2017

C++ shared_ptr, Aborted (core dumped), (double free or corruption, invalid next size )

I have a large C++ project and i'm using std::shared_ptr, std::enable_shared_from_this and std::static_pointer_cast a lot. I'm not very familiar with pointers but i have to use them.

So everything worked fine until today: my programm started crashing when my vectors size increased. For example i used to work with

std::vector<std::vector<std::shared_ptr<State> > > trans_prob(16);
for(unsigned int i=0;i<trans_prob.size();i++)
   trans_prob[i]=std::vector<std::shared_ptr<State> >(4);
...

but now my vector size is about 480 x 16.

I don't get the same error when i execute the programm multiple times, sometimes i get

*** Error in `...': free(): invalid next size (fast): 0x0000000001977d50 ***

sometimes

*** Error in `...': double free or corruption (!prev): 0x0000000002672b80 ***

If i debug the code in QtCreator the debugger stops always at a different line.

Here a example Backtrace: here i got a "double free or corruption" - Error

Thread 1 (Thread 0x7f6485db3540 (LWP 6867)):
#0  0x00007f6482560428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
        resultvar = 0
        pid = 6867
        selftid = 6867
#1  0x00007f648256202a in __GI_abort () at abort.c:89
        save_stage = 2
        act = {__sigaction_handler = {sa_handler = 0x62696c2f7273752f, sa_sigaction = 0x62696c2f7273752f}, sa_mask = {__val = {3257288213055174703, 7955377262162766188, 8314053253615333237, 3328774130788872812, 3919881042654277168, 3472332728684590644, 3834309541000326448, 8223625903106832691, 3472328295963457581, 4195155967701168176, 3690196551073804848, 2314885530818453554, 2314885530818453536, 7795484802351636512, 3917909816998060649, 3276497845987585332}}, sa_flags = 796225127, sa_restorer = 0x91}
        sigs = {__val = {32, 0 <repeats 15 times>}}
#2  0x00007f64825a27ea in __libc_message (do_abort=do_abort@entry=2, fmt=fmt@entry=0x7f64826bb2e0 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:175
        ap = <error reading variable ap (Attempt to dereference a generic pointer.)>
        fd = 5
        on_2 = <optimized out>
        list = <optimized out>
        nlist = <optimized out>
        cp = <optimized out>
        written = <optimized out>
#3  0x00007f64825aae0a in malloc_printerr (ar_ptr=<optimized out>, ptr=<optimized out>, str=0x7f64826bb410 "double free or corruption (!prev)", action=3) at malloc.c:5004
        buf = "0000000002672b80"
        cp = <optimized out>
        ar_ptr = <optimized out>
        str = 0x7f64826bb410 "double free or corruption (!prev)"
        action = 3
#4  _int_free (av=<optimized out>, p=<optimized out>, have_lock=0) at malloc.c:3865
        size = <optimized out>
        fb = <optimized out>
        nextchunk = <optimized out>
        nextsize = <optimized out>
        nextinuse = <optimized out>
        prevsize = <optimized out>
        bck = <optimized out>
        fwd = <optimized out>
        errstr = <optimized out>
        locked = <optimized out>
#5  0x00007f64825ae98c in __GI___libc_free (mem=<optimized out>) at malloc.c:2966
        ar_ptr = <optimized out>
        p = <optimized out>
        hook = <optimized out>
#6  0x000000000042d2cc in __gnu_cxx::new_allocator<double>::deallocate (this=0x265d9e0, __p=0x2672b80) at /usr/include/c++/5/ext/new_allocator.h:110
No locals.
#7  0x000000000042c537 in std::allocator_traits<std::allocator<double> >::deallocate (__a=..., __p=0x2672b80, __n=48) at /usr/include/c++/5/bits/alloc_traits.h:517
No locals.
#8  0x000000000042b2a8 in std::_Vector_base<double, std::allocator<double> >::_M_deallocate (this=0x265d9e0, __p=0x2672b80, __n=48) at /usr/include/c++/5/bits/stl_vector.h:178
No locals.
#9  0x000000000042a945 in std::_Vector_base<double, std::allocator<double> >::~_Vector_base (this=0x265d9e0, __in_chrg=<optimized out>) at /usr/include/c++/5/bits/stl_vector.h:160
No locals.
#10 0x00000000004296c5 in std::vector<double, std::allocator<double> >::~vector (this=0x265d9e0, __in_chrg=<optimized out>) at /usr/include/c++/5/bits/stl_vector.h:425
No locals.
#11 0x0000000000428494 in kukadu::Custom_Vector::~Custom_Vector (this=0x265d9d0, __in_chrg=<optimized out>) at /home/ivan/Uni/robot_sw/iis_catkin_ws/src/kukadu/include/kukadu/learning/rl/types.hpp:23
No locals.
#12 0x000000000042e7a0 in std::_Sp_counted_ptr<kukadu::Custom_Vector*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x265b990) at /usr/include/c++/5/bits/shared_ptr_base.h:374
No locals.
#13 0x000000000042a83a in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x265b990) at /usr/include/c++/5/bits/shared_ptr_base.h:150
No locals.
#14 0x00000000004294cd in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x7ffec988f828, __in_chrg=<optimized out>) at /usr/include/c++/5/bits/shared_ptr_base.h:659
No locals.
#15 0x00000000004284c0 in std::__shared_ptr<kukadu::Custom_Vector, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x7ffec988f820, __in_chrg=<optimized out>) at /usr/include/c++/5/bits/shared_ptr_base.h:925
No locals.
#16 0x00000000004284dc in std::shared_ptr<kukadu::Custom_Vector>::~shared_ptr (this=0x7ffec988f820, __in_chrg=<optimized out>) at /usr/include/c++/5/bits/shared_ptr.h:93
No locals.
#17 0x00007f648413acee in kukadu::Observation::equals (this=0x27d20d0, other=...) at /home/ivan/Uni/robot_sw/iis_catkin_ws/src/kukadu/include/kukadu/learning/rl/observation.hpp:22
No locals.
#18 0x00007f6484139254 in kukadu::pomdp_interface::find (this=0x2645850, observations=..., observation=...) at /home/ivan/Uni/robot_sw/iis_catkin_ws/src/kukadu/src/learning/rl/pomdp_interface.cpp:145
        i = 11
#19 0x00007f6484138d4f in kukadu::pomdp_interface::collectData (this=0x2645850, next_observation=..., reward=-1) at /home/ivan/Uni/robot_sw/iis_catkin_ws/src/kukadu/src/learning/rl/pomdp_interface.cpp:101
        depth = 32766
        new_belief = {<std::__shared_ptr<kukadu::Custom_Vector, (__gnu_cxx::_Lock_policy)2>> = {_M_ptr = 0x27d20d0, _M_refcount = {_M_pi = 0x27d20f0}}, <No data fields>}
#20 0x00007f6484138abd in kukadu::pomdp_interface::start (this=0x2645850, timesteps=50000) at /home/ivan/Uni/robot_sw/iis_catkin_ws/src/kukadu/src/learning/rl/pomdp_interface.cpp:75
        next_observation = {<std::__shared_ptr<kukadu::Observation, (__gnu_cxx::_Lock_policy)2>> = {_M_ptr = 0x27d20d0, _M_refcount = {_M_pi = 0x27d20f0}}, <No data fields>}
        trial = 45
        step = 391
        reward = -1
#21 0x0000000000426a7d in main (argc=1, args=0x7ffec988fb88) at /home/ivan/Uni/robot_sw/iis_catkin_ws/src/kukadu_tutorials/src/cliffwalk_example.cpp:258
        terminal_state = true
        gt = {<std::__shared_ptr<kukadu::Custom_Matrix3D, (__gnu_cxx::_Lock_policy)2>> = {_M_ptr = 0x262c150, _M_refcount = {_M_pi = 0x2641040}}, <No data fields>}
        env = {<std::__shared_ptr<kukadu::Grid_Environment, (__gnu_cxx::_Lock_policy)2>> = {_M_ptr = 0x26475c0, _M_refcount = {_M_pi = 0x2647650}}, <No data fields>}
        pomdp = {<std::__shared_ptr<kukadu::pomdp_interface, (__gnu_cxx::_Lock_policy)2>> = {_M_ptr = 0x2645850, _M_refcount = {_M_pi = 0x27d20b0}}, <No data fields>}
        anz_steps = 6563016
        anz_iterations = 0
        reduction = 2.1690672550637201e-317

edit: i sometimes resize the Matrix during runtime, but i also tried to copy the whole Matrix into a new bigger matrix and i get the same error.

Aucun commentaire:

Enregistrer un commentaire