Friday 15 May 2015

c++ - function crashes depending on function argument definition - double free or corruption -



c++ - function crashes depending on function argument definition - double free or corruption -

i have defined class evaluation of bspline basis functions. have used pointers, or new delete etc. class following:

class bspline_basis{ //private: public: int k; /*! order of bspline basis */ int nbreak; /*! dimension of breakpoints vector. */ int nknots; /*! dimension of knots vector. */ int nbasis; /*! number of basis functions */ vector<double> breakpts; /*! represents strictly increasing values of knots, excluding multiplicities */ vector<double> knots; /*! raw knot vector of bspline basis, may contain multiple entries due multiplicity */ vector<double> bix_nonzero; /*! size: (k). stores nonzero components of bspline basis */ vector<double> bix; /*! size: nbasis stores components of bspline basis. not necessary - remove? */ int find_knot_span_of_x(const double &x); /*! returns integer i: t_i <= x < t_{i+k}. upon phone call stores in i_saved */ pair<int,int> find_nonzero_basis_at_x(const double &x); /*! returns first, lastly index of nonzero basis b_i(x) @ particular x. */ pair<int,int> find_base_nonzero_interval(const double &x); /*! returns first (i) , lastly (i+k) index of knots t_i @ particular x. */ int i_saved; // temporary saves speed double x_saved; // temporary saves speed /* !essential routines evaluation! add together optional argument knot vector utilize in evaluation of integrals */ void eval_nonzero_basis(const int &i, const double &x); /*! evaluates non 0 basis functions @ x */ void eval_bix(const int &i, const double &x); /*! evaluates basis functions @ x */ /*! default clamped knot vector constructor */ bspline_basis(const vector<double> &_breakpts, const int &_k); /* evaluation functions */ double get_bix(const int &i, const double &x); /*! value b_i(x) */ };

the function transparent user , evaluates functions b_i(x) is

get_bix(const int &i, const double &x);

when utilize within loop, variable integer i, works great, is, in example:

// constructor of class mybasis for(double x=0; x<=10. x+=0.01) { cout<< x << " "; (int i=0; i<nbasis; ++i) cout<< mybasis.get_bix(i,x)<<" "; cout<<endl; }

correct values printed. if define constant integer first argument of function, in example:

int idx=3; for(double x=0; x<=10. x+=0.01) { cout<< x << " "; //for (int i=0; i<nbasis; ++i) cout<< mybasis.get_bix(idx,x)<<" "; cout<<endl; }

i next error:

*** error in `./test_class.xxx': double free or corruption (out): 0x0000000000740280 ***

when run code in gdb, , backtrace it, next message:

(gdb) bt #0 0x00007ffff7530bb9 in __gi_raise (sig=sig@entry=6) @ ../nptl/sysdeps/unix/sysv/linu/raise.c:56 #1 0x00007ffff7533fc8 in __gi_abort () @ abort.c:89 #2 0x00007ffff756de14 in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff767c668 "*** error in `%s': %s: 0x%s ***\n") @ ../sysdeps/posix/libc_fatal.c:175 #3 0x00007ffff757a0ee in malloc_printerr (ptr=<optimised out>, str=0x7ffff767c798 "double free or corruption (out)", action=1) @ malloc.c:4996 #4 _int_free (av=<optimised out>, p=<optimised out>, have_lock=0) @ malloc.c:3840 #5 0x00000000004039ac in __gnu_cxx::new_allocator<double>::deallocate (this=0x7fffffffdc70, __p=0x608280) @ /usr/include/c++/4.8/ext/new_allocator.h:110 #6 0x00000000004031be in std::_vector_base<double, std::allocator<double> >::_m_deallocate (this=0x7fffffffdc70, __p=0x608280, __n=15) @ /usr/include/c++/4.8/bits/stl_vector.h:174 #7 0x00000000004030b3 in std::_vector_base<double, std::allocator<double> >::~_vector_base (this=0x7fffffffdc70, __in_chrg=<optimised out>) @ /usr/include/c++/4.8/bits/stl_vector.h:160 #8 0x00000000004028ed in std::vector<double, std::allocator<double> >::~vector (this=0x7fffffffdc70, __in_chrg=<optimised out>) @ /usr/include/c++/4.8/bits/stl_vector.h:416 #9 0x00000000004017aa in bspline_basis::eval_bix (this=0x7fffffffdd40, ii=4, x=@0x7fffffffdd10: 0.01) @ bsplines_stackoverflow.hpp:247 #10 0x0000000000401f59 in bspline_basis::get_bix (this=0x7fffffffdd40, i=4, x=@0x7fffffffdd10: 0.01) @ bsplines_stackoverflow.hpp:331 #11 0x000000000040214e in main () @ test_class.cpp:31

the problem must lie in functions

double bspline_basis::get_bix(const int &i, const double &x) { if (i<0 || i> nbasis){ debug(i); std::cerr<< "index of bix out of range, aborting ..." << endl; throw 0; } if (x==x_saved && i==i_saved) { homecoming bix[i]; }else if ( x != x_saved && == i_saved){ eval_bix(i_saved,x); // evaluate nonzero , store bix. x_saved=x; // store x subsequent evaluations. homecoming bix[i]; }else { // a. find knot span of x: find_knot_span_of_x(x); // b. evaluate nonzero bix , pass them bix: eval_bix(i_saved,x); x_saved=x; // store x subsequent evaluations. i_saved=i; // store knot span possible subsequent evaluations. homecoming bix[i]; } }

and

/*! wrapper function eval_nonzero_basis. passes nonzero basis values vector<double> bix. */ void bspline_basis::eval_bix (const int &ii, const double &x){ //pair<int,int> i_start_end = find_nonzero_basis_at_x(x); int istart= ii-k+1; pair<int,int> i_start_end = make_pair(istart,ii); // evaluate nonzero entries. index. eval_nonzero_basis(i_start_end.second, x); // initialize (to zeros) temporary vector of dimension nbasis vector<double> bix_temp(nbasis,0.0); // pass nonzero entries temporary vector for(int j= i_start_end.first; j <= i_start_end.second; ++j) bix_temp[j] = bix_nonzero[j-i_start_end.first]; // move temporary vector bix bix=bix_temp; }

i cannot understand how possible error when defined first argument outside loop. help much appreciated.

update: please allow me clarify problem not because index (idx=3) outside allowed range interval. if case first loop crash. variable nbasis much greater 3.

update 2: next @adrian suggestion, i've made bix_temp element of class, , re-run code. 1 time again code crashes, know produces output (values x , infs). new output of debugger:

9.97 inf 9.98 inf 9.99 inf 10 inf 0.002867sec 4.78333e-05min *** error in `/home/foivos/documents/grav_ast/bsplines/genetic_algorithms/tests/test_new_jeans/bsplines_class/test_class.xxx': double free or corruption (out): 0x0000000000608180 *** programme received signal sigabrt, aborted. 0x00007ffff7530bb9 in __gi_raise (sig=sig@entry=6) @ ../nptl/sysdeps/unix/sysv/linux/raise.c:56 56 ../nptl/sysdeps/unix/sysv/linux/raise.c: no such file or directory. (gdb) bt #0 0x00007ffff7530bb9 in __gi_raise (sig=sig@entry=6) @ ../nptl/sysdeps/unix/sysv/linux/raise.c:56 #1 0x00007ffff7533fc8 in __gi_abort () @ abort.c:89 #2 0x00007ffff756de14 in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff767c668 "*** error in `%s': %s: 0x%s ***\n") @ ../sysdeps/posix/libc_fatal.c:175 #3 0x00007ffff757a0ee in malloc_printerr (ptr=<optimised out>, str=0x7ffff767c798 "double free or corruption (out)", action=1) @ malloc.c:4996 #4 _int_free (av=<optimised out>, p=<optimised out>, have_lock=0) @ malloc.c:3840 #5 0x00000000004039e8 in __gnu_cxx::new_allocator<double>::deallocate (this=0x7fffffffdda8, __p=0x608180) @ /usr/include/c++/4.8/ext/new_allocator.h:110 #6 0x00000000004031fa in std::_vector_base<double, std::allocator<double> >::_m_deallocate (this=0x7fffffffdda8, __p=0x608180, __n=15) @ /usr/include/c++/4.8/bits/stl_vector.h:174 #7 0x00000000004030ef in std::_vector_base<double, std::allocator<double> >::~_vector_base (this=0x7fffffffdda8, __in_chrg=<optimised out>) @ /usr/include/c++/4.8/bits/stl_vector.h:160 #8 0x0000000000402929 in std::vector<double, std::allocator<double> >::~vector (this=0x7fffffffdda8, __in_chrg=<optimised out>) @ /usr/include/c++/4.8/bits/stl_vector.h:416 #9 0x0000000000402646 in bspline_basis::~bspline_basis (this=0x7fffffffdd30, __in_chrg=<optimised out>) @ bsplines_stackoverflow.hpp:57 #10 0x00000000004022b6 in main () @ test_class.cpp:22 (gdb) frame 9 #9 0x0000000000402646 in bspline_basis::~bspline_basis (this=0x7fffffffdd30, __in_chrg=<optimised out>) @ bsplines_stackoverflow.hpp:57 57 class bspline_basis{ (gdb) info args = 0x7fffffffdd30 __in_chrg = <optimised out> (gdb)

again allow me emphasize when utilize loop index i don't errors.

update 3: found bug, conceptual , within function

get_bix(const int &i, const double &x)

specifically, in next lines of code:

// using same index i, different x, error , causing troubles. }else if ( x != x_saved && == i_saved){ eval_bix(i_saved,x); // evaluate nonzero , store bix. x_saved=x; // store x subsequent evaluations. homecoming bix[i]; }else {

thank help.

try:

if (i<0 || i>= nbasis){

instead of

if (i<0 || i> nbasis){

at top of get_bix.

i'm guessing you're in 3d, in case you'll have components 0, 1 , 2, not 3.

c++ bspline

No comments:

Post a Comment