c - Mex with memory leakage .. I need some help for spotting the error -


i trying make mex file of code. mex file created when try use mex file matlab crashes can locate error? code works fine in c language means issue in mex file creation..

  #include "mex.h"     void idwt(double z[2],float x[20][4], double row,double col, double of,double nv,double s, double i[9][8])     {         float *sum2;     float **sum;     float **sum1;     float *v;     float **b;     int *indexes;     double *idxl;     double **idx1;     float *dis;     double n=8;     double m=9;     int i,j,k,l,t,f,g,h;     int r=0;     int temp=0;     int f2=4;     int sum22;     int rmax,rmin,cmax,cmin;     int q=rmax*cmax;     float ktmp;     rmin=((row-s)<1)?1:(row-s);     rmax=(n<(row+s))?n:(row+s);     cmin=((col-s)<1)?1:(col-s);     cmax=(m<(col+s))?m:(col+s);      sum= (float *)mxmalloc(q*f2*sizeof(float));     sum1= (float *)mxmalloc(q*f2*sizeof(float));     sum2= (float *)mxmalloc(q*sizeof(float));     b= (float *)mxmalloc(q*f2*sizeof(float));        v=(float *)mxmalloc(f2*sizeof(float));     idx1= (double *)mxmalloc(rmax*cmax*sizeof(double));     idxl= (double *)mxmalloc(q*sizeof(double));     dis= (float *)mxmalloc(q*sizeof(float));     indexes= (int *)mxmalloc(q*sizeof(int));       (i = 0; < rmax; i++){          for( j = 0 ; j <cmax ; j++ ){           idx1[i][j]= i[i][j]; }}      (i = 0; < rmax; i++)         { (j = 0; j < cmax; j++)         {idxl[i + j*rmax] = idx1[i][j];}}       for(l=0; l<(rmax*cmax); l++)         {r=idxl[l]-1;         for(k=0; k<f2; k++)         {b[l][k]=x[r][k];}}       (i = 0,j=(of-1); <f2; i++)         {v[i]  = x[j][i]; }       (i = 0; i<f2 ; i++) {     (j = 0 ; j < (rmax*cmax); j++) {             sum[j][i] = b[j][i] - v[i];         sum1[j][i]= pow(sum[j][i],2); }}      (i = 0; <(rmax*cmax) ; i++) {         sum22=0;         (j = 0 ; j < f2; j++) {         sum22+=sum1[i][j];}         sum2[i]=sum22;}       for(i = 0; <(rmax*cmax) ; i++)         {    dis[i]= sum2[i]/f2;}        for( f = 0; f < rmax*cmax; f++)         indexes[f] = f;         for(g = 0; g < rmax*cmax; g++)         {for(h = 0; h < rmax*cmax; h++)         {if(dis[indexes[g]] < dis[indexes[h]])             {ktmp = indexes[g];                     indexes[g] = indexes[h];                     indexes[h] = ktmp;}}}      (j = 0; j <2; j++)        { temp=indexes[j];          z[j]=idxl[temp];}     (t = 0; t <2; t++)        { printf("%lf---\n", z[t]);}      mxfree (sum);     mxfree (sum1);     mxfree(sum2);     mxfree(b);       mxfree(v);     mxfree(idx1);     mxfree(idxl);     mxfree(dis);     mxfree(indexes);        }      void mexfunction(int nlhs, mxarray *plhs[], int nrhs, const mxarray *prhs[])     {      //declaring arguments      float *x;  double row;  double col;  double of;  double nv;  double s; double *i;     //double precision correspondance of output     double *z;      x = mxgetpr(prhs[0]);     row = mxgetscalar(prhs[1]);     col = mxgetscalar(prhs[2]);     of = mxgetscalar(prhs[3]);     nv = mxgetscalar(prhs[4]);     s = mxgetscalar(prhs[5]);     i= mxgetpr(prhs[6]);       plhs[0] = mxcreatedoublematrix(2,1,mxreal);      z = mxgetpr(plhs[0]);     idwt(z,x,row,col, of, nv, s,  i);      } 

here stack trace

matlab crash file:c:\users\abeera~1\appdata\local\temp\matlab_crash_dump.1808-1:   ------------------------------------------------------------------------        segmentation violation detected @ mon jun 01 11:47:58 2015 ------------------------------------------------------------------------  configuration:   crash decoding     : disabled   default encoding   : windows-1252   matlab architecture: win64   matlab root        : c:\program files\matlab\r2013a   matlab version     : 8.1.0.604 (r2013a)   operating system   : microsoft windows 8   processor id       : x86 family 6 model 42 stepping 7, genuineintel   virtual machine    : java 1.6.0_17-b04 sun microsystems inc. java hotspot(tm) 64-bit server vm mixed mode   window system      : version 6.2 (build 9200)  fault count: 1   abnormal termination: segmentation violation  register state (from fault):   rax = 3ff0000000000000  rbx = 00000000983d2080   rcx = 0000000000000001  rdx = 0000000000000008   rsp = 000000000403a470  rbp = 00000000983823a0   rsi = 0000000000000000  rdi = 0000000000000002     r8 = 00000000983823a0   r9 = 0000000000000002   r10 = 0000000000000000  r11 = 0000000000000000   r12 = 0000000000000002  r13 = 000000007d9fdba0   r14 = 0000000000000004  r15 = 0000000000000000    rip = 00007fff892111b3  efl = 00010202     cs = 0033   fs = 0053   gs = 002b      stack trace (from fault):     [  0] 0x00007fff892111b3      c:\users\abeera tariq\desktop\data\prp.mexw64+00004531     [  1] 0x00007fff89211ac2      c:\users\abeera tariq\desktop\data\prp.mexw64+00006850 mexfunction+00000210     [  2] 0x000000000442f731 c:\program files\matlab\r2013a\bin\win64\libmex.dll+00063281 mexrunmexfile+00000129     [  3] 0x000000000442e202 c:\program files\matlab\r2013a\bin\win64\libmex.dll+00057858 inswapmexfilereader+00000226     [  4] 0x000000000442f2b4 c:\program files\matlab\r2013a\bin\win64\libmex.dll+00062132 _constructor_lm_mexmaci64+00001620     [  5] 0x0000000074e8c64f c:\program files\matlab\r2013a\bin\win64\m_dispatcher.dll+00050767 mfh_file::dispatch_fh+00000623     [  6] 0x0000000074e8ced2 c:\program files\matlab\r2013a\bin\win64\m_dispatcher.dll+00052946 mfunction_handle::dispatch+00000802     [  7] 0x0000000004f5bc7b c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00834683 indestroyws+00392123     [  8] 0x0000000004f4b091 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00766097 indestroyws+00323537     [  9] 0x0000000004f3008f c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00655503 indestroyws+00212943     [ 10] 0x0000000004f465a4 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00746916 indestroyws+00304356     [ 11] 0x0000000004f3c3b0 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00705456 indestroyws+00262896     [ 12] 0x0000000004f2f112 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00651538 indestroyws+00208978     [ 13] 0x0000000004f30bd0 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00658384 indestroyws+00215824     [ 14] 0x0000000004f1cf79 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00577401 indestroyws+00134841     [ 15] 0x0000000074e8c64f c:\program files\matlab\r2013a\bin\win64\m_dispatcher.dll+00050767 mfh_file::dispatch_fh+00000623     [ 16] 0x0000000074e8ced2 c:\program files\matlab\r2013a\bin\win64\m_dispatcher.dll+00052946 mfunction_handle::dispatch+00000802     [ 17] 0x0000000004e9e70e c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00059150 invalidateloadedobject+00002350     [ 18] 0x0000000004fa6495 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+01139861 iinterpreter::iinterpreter+00000229     [ 19] 0x0000000004faca45 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+01165893 inregisterfunctionhandles+00004405     [ 20] 0x0000000004faca12 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+01165842 inregisterfunctionhandles+00004354     [ 21] 0x0000000004fac9d7 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+01165783 inregisterfunctionhandles+00004295     [ 22] 0x0000000004f9ba0e c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+01096206 ingetseqvarptrsfromcurrentws+00005742     [ 23] 0x0000000004e9be89 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00048777 inevalcmdwithlocalreturn+00000329     [ 24] 0x0000000004e9bdcc c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00048588 inevalcmdwithlocalreturn+00000140     [ 25] 0x0000000004e9bd81 c:\program files\matlab\r2013a\bin\win64\m_interpreter.dll+00048513 inevalcmdwithlocalreturn+00000065     [ 26] 0x0000000079fbbecd c:\program files\matlab\r2013a\bin\win64\libmwbridge.dll+00048845 mndebugprompt+00000109     [ 27] 0x0000000079fbd01b c:\program files\matlab\r2013a\bin\win64\libmwbridge.dll+00053275 mnparser+00000763     [ 28] 0x000000007499daa4   c:\program files\matlab\r2013a\bin\win64\mcr.dll+00187044 mcrinstance::mnparser_on_interpreter_thread+00000036     [ 29] 0x0000000074982220   c:\program files\matlab\r2013a\bin\win64\mcr.dll+00074272 mcr::setinterpreterthreadtocurrent+00031664     [ 30] 0x0000000074982280   c:\program files\matlab\r2013a\bin\win64\mcr.dll+00074368 mcr::setinterpreterthreadtocurrent+00031760     [ 31] 0x00000000057af9d6   c:\program files\matlab\r2013a\bin\win64\uiw.dll+00391638 uiw_isusermessage+00000086     [ 32] 0x00000000057b0095   c:\program files\matlab\r2013a\bin\win64\uiw.dll+00393365 uiw_setglimusermsg+00000117     [ 33] 0x00007fff8eeeb6f6                     c:\windows\system32\user32.dll+00571126 broadcastsystemmessageexa+00000570     [ 34] 0x00007fff8ee63a85                     c:\windows\system32\user32.dll+00014981 iswindow+00001653     [ 35] 0x00007fff8ee63af6                     c:\windows\system32\user32.dll+00015094 iswindow+00001766     [ 36] 0x00007fff8f4798ef                      c:\windows\system32\ntdll.dll+00628975 kiusercallbackdispatcher+00000031     [ 37] 0x00007fff8ee6303a                     c:\windows\system32\user32.dll+00012346 peekmessagew+00000202     [ 38] 0x00007fff8ee67433                     c:\windows\system32\user32.dll+00029747 getmessagea+00000251     [ 39] 0x00007fff8ee673d7                     c:\windows\system32\user32.dll+00029655 getmessagea+00000159     [ 40] 0x0000000005789a1b   c:\program files\matlab\r2013a\bin\win64\uiw.dll+00236059 uiw_setcurrentdialog+00000731     [ 41] 0x00000000057b1532   c:\program files\matlab\r2013a\bin\win64\uiw.dll+00398642 ws_freesystemfont+00000482     [ 42] 0x00000001800d8930 c:\program files\matlab\r2013a\bin\win64\libmwservices.dll+00887088 services::system_events::ppedispatchhook::~ppedispatchhook+00055600     [ 43] 0x00000001800d92f5 c:\program files\matlab\r2013a\bin\win64\libmwservices.dll+00889589 services::system_events::ppedispatchhook::~ppedispatchhook+00058101     [ 44] 0x00000001800da100 c:\program files\matlab\r2013a\bin\win64\libmwservices.dll+00893184 services::system_events::ppedispatchhook::~ppedispatchhook+00061696     [ 45] 0x00000001800e28d2 c:\program files\matlab\r2013a\bin\win64\libmwservices.dll+00927954 svws_processpendingevents+00001090     [ 46] 0x000000007498329d   c:\program files\matlab\r2013a\bin\win64\mcr.dll+00078493 mcr::setinterpreterthreadtocurrent+00035885     [ 47] 0x0000000074983562   c:\program files\matlab\r2013a\bin\win64\mcr.dll+00079202 mcr::setinterpreterthreadtocurrent+00036594     [ 48] 0x0000000074976915   c:\program files\matlab\r2013a\bin\win64\mcr.dll+00026901 mcrinstantiationerror::operator=+00003781     [ 49] 0x0000000074979d52   c:\program files\matlab\r2013a\bin\win64\mcr.dll+00040274 mcr_invoke_on_interpreter_thread+00001602     [ 50] 0x0000000140005591 c:\program files\matlab\r2013a\bin\win64\matlab.exe+00021905     [ 51] 0x0000000140005c3a c:\program files\matlab\r2013a\bin\win64\matlab.exe+00023610     [ 52] 0x0000000140006103 c:\program files\matlab\r2013a\bin\win64\matlab.exe+00024835     [ 53] 0x00007fff8d601611                   c:\windows\system32\kernel32.dll+00005649 basethreadinitthunk+00000013     [ 54] 0x00007fff8f4564ad                      c:\windows\system32\ntdll.dll+00484525 rtluserthreadstart+00000029   error detected while mex-file running. if mex-file not official mathworks function, please examine source code errors. please consult external interfaces guide information on debugging mex-files.  if problem reproducible, please submit service request via:     http://www.mathworks.com/support/contact_us/  technical support engineer might contact further information.  thank help. 

tentative answer, @ piece:

for (i = 0; < rmax; i++){      for( j = 0 ; j <cmax ; j++ ){      idx1[i][j]= i[i][j]; }} 

i [9][8], cmax can go 9 (m = 9):

cmax=(m<(col+s))?m:(col+s); 

so, you're accessing element out of bounds.

2nd possibility:

int q=rmax*cmax; 

q declared before rmax , cmax allocated values. allocation using q might issue.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -