free() and mxFree() in MATLAB - freeing memory twice -
good day, have following code has given me problems day already. have debugged it, , works fine until trying free memory. free() function should called @ end of execution automatically, commented mxfree() code out, in hope of getting result. if that, program frees memory twice, in case of manually freeing memory - , conclude beyond control.
*** glibc detected *** /usr/local/matlab/r2012a/bin/glnx86/matlab: free(): invalid pointer: 0xad2427a1 *** is there missed?
note: have tried examples of .mex files memory allocation , work fine - mistake down below, in code.
/* beamforming algorithm arguments: xr, yr, zr, t, ts, w, u, sdata, nrsensor c, omega_o output: s1_sum s1_sum = beamforming(xr,yr,zr,t,ts,w,u,sdata,nrsensor,c,omega_o) */ #include "mex.h" #include <stdlib.h> #include <math.h> #define input_args 11 #define output_args 1 #define ar(a, b) ar[b+a*nrsensor] #define ai(a, b) ai[b+a*nrsensor] #define v(a, b) v[b+a*len_zr] #define tau(a, b) tau[b+a*nrsensor] #define tau_s(a, b) tau_s[b+a*len_zr] #define tau_r(a, b) tau_r[b+a*nrsensor] #define w(a, b) w[b+a*len_zr] #define w(a, b) w[b+a*nrsensor] #define arg(a, b) arg[b+a*len_zr] #define newrange(a, b) newrange[b+a*len_zr] #define oldrange(a, b) oldrange[b+a*len_zr] #define s1_sum_r(a, b, c) s1_sum_r[c+b*len_xr+a*len_yr*len_xr] #define s1_sum_i(a, b, c) s1_sum_i[c+b*len_xr+a*len_yr*len_xr] #define sdata_r(a, b) sdata_r[b+a*nrsensor] #define sdata_i(a, b) sdata_i[b+a*nrsensor] #define s1interpr(a, b) s1interpr[b+a*len_zr] #define s1interpi(a, b) s1interpi[b+a*len_zr] #define pi_ 3.141592653 double sinc(double x){ return sin(pi_*x)/(pi_*x); } void mexfunction(int nlhs, mxarray * plhs[], int nrhs, const mxarray * prhs[]) { /* declarations */ int xr_, yr_, i, j, m; int len_xr, len_yr, len_zr, len_t, len_t_, nrsensor, nrsensor_cen; double _t, _i, arg_min, arg_max, norm_factor; double start_tau, end_tau, c, omega_o; double *xr, *yr, *zr, *t, *ts, *sdata_r, *sdata_i, *w; double *ar, *ai, *v, *tau, *tau_r, *tau_s, *arg, *u, *w, *t_, *x_r, *x_i, *start_index, *newrange, *oldrange, *s1_sum_r, *s1_sum_i, *s1interpi, *s1interpr; /* checking number of arguments */ if(nrhs != input_args) mxerrmsgtxt("incorrect number of arguments!\n"); if(nlhs != output_args) mxerrmsgtxt("incorrect number of outputs!\n"); /* reading arguments */ xr = mxgetpr(prhs[0]); yr = mxgetpr(prhs[1]); zr = mxgetpr(prhs[2]); t = mxgetpr(prhs[3]); ts = mxgetpr(prhs[4]); w = mxgetpr(prhs[5]); u = mxgetpr(prhs[6]); sdata_r = mxgetpr(prhs[7]); sdata_i = mxgetpi(prhs[7]); nrsensor = (int) mxgetscalar(prhs[8]); c = mxgetscalar(prhs[9]); omega_o = mxgetscalar(prhs[10]); len_xr = mxgetn(prhs[0]); len_yr = mxgetn(prhs[1]); len_zr = mxgetn(prhs[2]); len_t = mxgetm(prhs[3]); /* initialisations */ _t = 0.0; len_t_ = 0; nrsensor_cen = nrsensor/2; /* space allocation , checking */ arg = malloc(sizeof(double)*len_zr*len_t); ar = malloc(sizeof(double)*len_zr*nrsensor); ai = malloc(sizeof(double)*len_zr*nrsensor); v = malloc(sizeof(double)*len_zr*3); tau = malloc(sizeof(double)*len_zr*nrsensor); tau_s = malloc(sizeof(double)*len_zr*3); tau_r = malloc(sizeof(double)*len_zr*nrsensor); u = malloc(sizeof(double)*3); w = malloc(sizeof(double)*len_zr*3); w = malloc(sizeof(double)*nrsensor*3); ts = malloc(sizeof(double)*len_zr); t = malloc(sizeof(double)*len_t); t_ = malloc(sizeof(double)*len_t); x_r = malloc(sizeof(double)*len_t); x_i = malloc(sizeof(double)*len_t); arg = malloc(sizeof(double)*len_zr*len_t); newrange = malloc(sizeof(double)*len_zr*len_t); oldrange = malloc(sizeof(double)*len_zr*len_t); s1interpr = malloc(sizeof(double)*nrsensor*len_zr); s1interpi = malloc(sizeof(double)*nrsensor*len_zr); start_index = malloc(sizeof(double)*len_t); /* s1_sum_r = mxmalloc(len_xr*len_yr*len_zr); s1_sum_i = mxmalloc(len_xr*len_yr*len_zr); */ int dim_s1_sum[3] = {len_xr, len_yr, len_zr}; plhs[0] = mxcreatenumericarray(3, dim_s1_sum, mxdouble_class, mxcomplex); s1_sum_r = (double*) mxgetpr(plhs[0]); s1_sum_i = (double*) mxgetpi(plhs[0]); if(arg == null || ar == null || ai == null || v == null || tau == null || tau_s == null || tau_r == null || u == null || w == null || w == null || ts == null || t == null || t_ == null || x_r == null || x_i == null || start_index == null || newrange == null || oldrange == null || s1_sum_r == null || s1_sum_i == null || s1interpr == null|| s1interpi == null){ mxerrmsgtxt("malloc error!\n"); return; } /* --- initialising s1interp, s1_sum, tau, full of zeros */ for(i=0; i<nrsensor; i++){ for(j=0; j<len_zr; j++){ s1interpr(i,j) = 0; s1interpi(i,j) = 0; } } for(i=0; i<len_xr; i++){ for(j=0; j<len_yr; j++){ for(m=0; m<len_zr; m++){ s1_sum_r(i,j,m) = 0; s1_sum_i(i,j,m) = 0; } } } for(i=0; i<nrsensor; i++){ for(j=0; j<len_zr; j++){ tau(i,j) = 0; } } /* --- main algorithm --- */ for(xr_=0; xr_ < len_xr; xr_++){ for(yr_=0; yr_ < len_yr; yr_++){ for(i=0; < len_zr; i++){ v(0, i) = xr[xr_]; v(1, i) = yr[yr_]; v(2, i) = zr[i]; } for(i=0; < len_zr; i++){ tau_s(0, i) = v(0, i) - u[0]; tau_s(1, i) = v(1, i) - u[1]; tau_s(2, i) = v(2, i) - u[2]; } for(m=0; m < nrsensor; m++){ for(i=0; < len_zr; i++){ /* see no point of squeeze function since w of known sizes */ w(0, i) = v(0, i) - w(0, m); w(1, i) = v(1, i) - w(1, m); w(2, i) = v(2, i) - w(2, m); } for(i=0; i< len_zr; i++){ /* sum(w.*w) */ _t = w(0, i)*w(0,i) + w(1, i)*w(1,i) + w(2, i)*w(2,i); tau_r(m, i) = sqrt(_t)/c; } } for(m=0; m < len_zr; m++){ for(i=0; < nrsensor; i++){ /* computing sum(tau_s(m, :).*tau_s(m, :)) */ _t = tau_s(0, m)*tau_s(0, m) + tau_s(1, m)*tau_s(1, m) + tau_s(2, m)*tau_s(2, m); tau(i, m) = tau_r(i, m) + sqrt(_t)/c; } } /* for(i=0; < len_zr; i++){ for(j=0; j < nrsensor; j++){ tau(i,j)=tau(i,j); } } */ for(i=0; < len_zr; i++){ for(j=0; j < nrsensor; j++){ ar(i,j)=cos(omega_o * tau(i, j)); ai(i,j)=sin(omega_o * tau(i, j)); } } /* --- big loop ahead --- */ for(m=0; m < nrsensor; m++){ start_tau = tau(nrsensor_cen, 1); end_tau = tau(nrsensor_cen, len_zr); /* finding index : writing start indexes , t_ array */ len_t_=0; for(i=0; i<len_t; i++){ if(t[i] >= start_tau && t[i] <= end_tau){ start_index[len_t_] = i; t_[len_t_] = t[i]; x_r[len_t_] = sdata_r(i, m); x_i[len_t_] = sdata_i(i, m); len_t_++; } } for(i=0; < len_zr; i++){ ts[i]=tau(m, i); } for(i=0; < len_t_; i++){ for(j=0; j < len_zr; j++){ newrange(i, j) = ts[j]; } } for(i=0; < len_zr; i++){ for(j=0; j < len_t_; j++){ oldrange(j, i) = t_[j]; } } for(i=0; < len_t_; i++){ for(j=0; j < len_zr; j++){ arg(i, j) = newrange(i, j)-oldrange(i, j); } } arg_min = arg[0]; for(i=0; < len_t_; i++) for(j=0; j < len_zr; j++) if(arg_min>arg(i, j)) arg_min=arg(i, j); arg_max = arg[0]; for(i=0; < len_t_; i++) for(j=0; j < len_zr; j++) if(arg_max<arg(i, j)) arg_max=arg(i, j); norm_factor = (2*len_t_)/(arg_max-arg_min); for(i=0; < len_zr; i++){ _t = 0; for(j=0; j < nrsensor; j++){ _t = sinc(arg(i, j)*norm_factor)*x_r[j]; } s1interpr(m, i) = ar(m, i) * _t; _t = 0; for(j=0; j < nrsensor; j++){ _t = sinc(arg(i, j)*norm_factor)*x_i[j]; } s1interpi(m, i) = ai(m, i) * _t; } } for(i=0; < len_zr; i++){ _t = 0; for(j=0; j < nrsensor; j++){ _t += s1interpr(j, i); } s1_sum_r(xr_, yr_, i) = _t; _t = 0; for(j=0; j < nrsensor; j++){ _t += s1interpi(j, i); } s1_sum_i(xr_, yr_, i) = _t; } } } free(arg); free(ar); free(ai); free(v); free(tau); free(tau_s); free(tau_r); free(u); free(w); free(w); free(ts); free(t); free(t_); free(x_r); free(x_i); free(newrange); free(oldrange); free(s1interpr); free(s1interpi); free(start_index); return; } edit
i have deleted code except beginning (up memory allocation) , memory freeing. i'm using malloc() , free() now. in between memory allocation , freeing, have put code:
for(i=0; i<nrsensor; i++){ for(j=0; j<len_zr; j++){ s1interpr(i,j) = 0; s1interpi(i,j) = 0; } } for(i=0; i<nrsensor; i++){ for(j=0; j<len_zr; j++){ tau(i,j) = 0; } } the first loop causes no problem. second 1 though, apparently corrupts variable v (declared before it) , 1 declared after it. , seems, according logic, not exceed kind of bounds...
the second loop not index correctly tau. defining indexing tau as
#define tau(a, b) tau[b+a*nrsensor] let walk through second loop assuming nrsensor = 10 , len_zr = 5. case max value of loop variable 9 , max value of loop variable j 4. now,
tau(9,4) => tau[4+9*10] => tau[94]. but allocating tau with
tau = malloc(sizeof(double)*len_zr*nrsensor); which sample values 10 , 5 is
tau = malloc(sizeof(double)*50) you either need change indexing definition tau swap , b or change order of loops , j.
Comments
Post a Comment